CodeIgniter Forums
Duplicated methods - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Duplicated methods (/showthread.php?tid=1992)



Duplicated methods - El Forum - 07-10-2007

[eluser]Lark[/eluser]
Code:
class Test extends Controller
{
    function mymethod()
    {
        echo "My method with no parameters";
    }

    function mymethod($id)
    {
        echo "My method with one parameter: " . $id;
    }
}
Is this possible to do with codeigniter? I don't know what you call it, overloading of methods?

If i request: /test/mymethod/
"My method with no parameters" will be returned.

And if i try: /test/mymethod/45
"My method with one parameter: 45" will be returned.


I have tried but with no success. If it's not possible, maybe this would be a good future implementation?


Duplicated methods - El Forum - 07-10-2007

[eluser]Rick Jolly[/eluser]
What you are describing is method overloading as it applies to most OOP programming languages. PHP's definition of method overloading is totally different. You can use default parameters instead:
Code:
function mymethod($id = null)
{
   if (! isset($id))
   {
      echo "My method with no parameters";
   }
   else
   {
      echo "My method with one parameter: " . $id;
   }
}



Duplicated methods - El Forum - 07-11-2007

[eluser]Lark[/eluser]
Ok, then I'll try that method. Thanks.


Duplicated methods - El Forum - 07-18-2007

[eluser]marcoss[/eluser]
[quote author="Rick Jolly" date="1184111418"]What you are describing is method overloading as it applies to most OOP programming languages. PHP's definition of method overloading is totally different. You can use default parameters instead:
Code:
function mymethod($id = null)
{
   if (! isset($id))
   {
      echo "My method with no parameters";
   }
   else
   {
      echo "My method with one parameter: " . $id;
   }
}
[/quote]

That works as he pretends, but it is definitely not method overloading.

You should check this out if you want to know the PHP way of doing it http://www.php.net/manual/en/language.oop5.overloading.php


Duplicated methods - El Forum - 07-18-2007

[eluser]Rick Jolly[/eluser]
[quote author="marcoss" date="1184777530"]
You should check this out if you want to know the PHP way of doing it http://www.php.net/manual/en/language.oop5.overloading.php[/quote]

Since PHP's version of "method overloading" (really unfortunate name) had nothing to do with Lark's question, discussing it wouldn't have been helpful.