Overloading and Overriding in PHP with Examples

Overloading and Overriding are forms of polymorphism in OOP.  According to Object Oriented Programming (OOP) concept if a class has methods of the same name but different parameters then we say that we are overloading that method. Also if we were to create a method in the child class having the same name, same number of parameters and the same access specifier as in its parent then we can say that we are doing method overriding.

Also, read:

As we know that PHP is not type strict, means if we implement overloading in C++ or Java, the function will look like add(int,float) is different from add(float,int) or even add(float,int,int) but it is not possible in PHP. Actually polymorphism is not easy in PHP. In this post, you will learn how to handle polymorphism in PHP.

1. Method Overloading

As discussed above PHP does not support method overloading compared to other language like Java or C++. For example:
class ABC {
public function displayMessage($para1) {
echo "Ffirst function displayMessage with parameter as para1";
}
public function displayMessage($para1,$para2) {
echo "Second function displayMessage with parameter as para1\2";
}
}
$obj1 = new ABC;
$obj1->displayMessage('Hello');

if above example code convert to Java or C++, it will work without any errors. But if we run above code, it will throw error the error “Cannot redeclare ABC::displayMessage()”. Simple overloading is not supported by PHP. But you can implement overloading by using the PHP magic method __call().


Example:
class ABC {
public function __call($method_name, $arguments) {
$methodArray = array('displayMessage1','displayMessage2');
if (in_array($method_name,$methodArray) === false) {
die("\n Method does not exist");
}
if (count($arguments) === 2) {
$this->displayMessage2($arguments[0],$arguments[1]);
}
elseif (count($arguments) === 1) {
$this->displayMessage1($arguments[0]);
} else {
echo "\n unknown method";
return false;
}
}
function displayMessage1($var1) {
echo "\n from func1($a)";
}
function displayMessage2($var1,$var2) {
echo "\n from func2($var1,$var2)";
}
}
$obj1 = new ABC;
$obj1->displayMessage1('hello');
$obj1->displayMessage2('hello','hello2');
$obj1->displayMessage3('Hello');

__call() function is triggered when invoking inaccessible methods in an object context. The syntax for __call() is mixed __call(string $name,array $arguments).
The $name parameter is the name of the method being called. The $arguments parameter is an enumerated array containing parameters passed to the $name method.

2. Method Overriding

If you create a method in the child class having the same name, same number of parameters and the same access specifier as in it’s parent then we can say that we are doing method overriding in PHP. for example:

class AA {
public function output($args) {
echo "\n Parent - the parameter value is $args";
}
}
class BB extends AA {
public function output($args) {
echo "\n Child - the parameter value is $args";
}
}
$obj1 = new AA;
$obj2 = new BB;
$obj1->output('class AA');
$obj2->output('class BB');

As according to polymorphism, if we invoke a method by inspecting the object at run time is a case of polymorphism. Here in above example either we call method output () of a class AA or class BB is depend on object which is making the call to the method.


You may also like:

One thought on “Overloading and Overriding in PHP with Examples

Comments are closed.