Welcome Guest, Not a member yet? Register   Sign In
Duplicated methods
#1

[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?
#2

[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;
   }
}
#3

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

[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.oo...oading.php
#5

[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.oo...oading.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.




Theme © iAndrew 2016 - Forum software by © MyBB