5

Does php support method overloading. While trying below code it suggests it supports method overloading. Any views

class test
{
  public test($data1)
  {
     echo $data1;
  }
}

class test1 extends test
{
    public test($data1,$data2)
    {
       echo $data1.' '.$data2;
    }
}

$obj = new test1();
$obj->test('hello','world');

As i have overload the method it gives the output as "hello world". Above code snippet suggests php supports method overloading. So my question is does php support method overloading.

user1189074
  • 51
  • 1
  • 1
  • 3
  • Check this answer http://stackoverflow.com/a/4697712/387194, in your case you don't use overloading but overwriting. The old function is not executed if you write `$obj->test('hello');` – jcubic Jun 26 '13 at 09:23
  • Refer : http://stackoverflow.com/questions/4697705/php-function-overloading – Arvind Jun 26 '13 at 09:24

3 Answers3

10

You should make the difference between method overriding (your example) and method overloading

Here is a simple example how to implement method overloading in PHP using __call magic method:

class test{
    public function __call($name, $arguments)
    {
        if ($name === 'test'){
            if(count($arguments) === 1 ){
                return $this->test1($arguments[0]);
            }
            if(count($arguments) === 2){
                return $this->test2($arguments[0], $arguments[1]);
            }
        }
    }

    private function test1($data1)
    {
       echo $data1;
    }

    private function test2($data1,$data2)
    {
       echo $data1.' '.$data2;
    }
}

$test = new test();
$test->test('one argument'); //echoes "one argument"
$test->test('two','arguments'); //echoes "two arguments"
ilyes kooli
  • 11,959
  • 14
  • 50
  • 79
  • How do I make one of them protected and the other public? It seems that if __call needs to be public, that kind of opens the gates wide? Use case, public getter, protected setter both with the same name as the data property... or you might have to dissect caller info (might get a bit verbose) –  Dec 23 '14 at 04:43
  • Whether I can have multiple call functions here? – Shan Apr 18 '15 at 17:07
  • Please develop what you mean by *multiple call functions*. If is out of the scope of this question, please post it as a new question. I am sure you will get an accurate answer. – ilyes kooli Apr 20 '15 at 13:44
1

So my question is does php support method overloading(?).

Yes, but not in that way, and, in your example, it not suggests that this kind of overloading is correct, at least with the version 5.5.3 of it and error_reporting(E_ALL).

In that version, when you try to run this code, it gives you the following messages:

Strict Standards: Declaration of test1::test() should be compatible
with test::test($data1) in /opt/lampp/htdocs/teste/index.php on line 16

Warning: Missing argument 1 for test::test(), called in /opt/lampp/htdocs/teste/index.php 
on line 18 and defined in /opt/lampp/htdocs/teste/index.php on line 4

Notice: Undefined variable: data1 in /opt/lampp/htdocs/teste/index.php on line 6
hello world //it works, but the messages above suggests that it's wrong.
Rafael Barros
  • 1,043
  • 18
  • 25
0

You forgot to add 'function' before test in both cases. Method is called of child class because when you call a method from child class object it first check if that method exist in child class, if not then it look into inherited parent class with visibility public or protected check and if method is exist than return the result according to that.

Kapil Yadav
  • 650
  • 1
  • 5
  • 29