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.
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:
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.
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: