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.

2

3 Answers 3

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"
Sign up to request clarification or add additional context in comments.

3 Comments

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)
Whether I can have multiple call functions here?
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.
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.

Comments

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.

1 Comment

In PHP, the use of the function keyword is not required, and does not make a difference in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.