Overloading and Overriding in PHP March 24
Overloading and Overriding in PHP March 24
Function or method Overloading and overriding method is very basic and useful feature of any oop language.
In this tutorial we will discuss implementation of method overloading and overriding in php. Here first we
will explore basics of overloading and overriding. After exploration of basics we will implement overloading
and overriding in php. Before going further I am assuming that you have basic knowledge of classes and
inheritance in php. Also you have understanding about magic method in php. Magic method because
overloading in php can be implmented using magic methods.
Normally method overriding required when your parent class have some method, but in your child class you
want the same method with different behavior. By overriding of method you can complete change its
behavior from parent class. To implment method overiding in oop we commonly create same method in child
class.
Normally method overloading in oop is managed on the basis of the argument passed in function. We can
achieve overloading in oop by providing different argument in same function.
Implementation of overriding in php is very easy. If your parent class has a function. You can create function
with same name in your child class to override the function. Implementation of overriding can not be
achieved by creating 2 function with same name and different argument in php. Because we can not create
same name function more than 1 time in php class. To implement overloading we need to take help of magic
method in php. In below section we will explore overloading and overriding one by one.
Overloading in PHP
As we know that we can not implement overloading by create 2 function in with same name in class. So
to implement overloading in php we will take help of magic method __call. Magic method __call invoked
when method called by class object is not available in class. So here we will not create method exactly and
will take help of __call method. Now call method will provide us 2 argument, 1st name of the method called
and parameter of the function. Now with the help of either switch , case or if else we
will implement overloading in php. Following is very simple example of overloading in php.
class test
{
public function __construct()
{
//Your logic for constructor
}
public function __call($method_name , $parameter)
{
if($method_name == "overlodedFunction") //Function overloading logic for function name
overlodedFunction
{
$count = count($parameter);
switch($count)
{
case "1":
//Business log in case of overlodedFunction function has 1 argument
echo "You are passing 1 argument";
break;
case "2": //Incase of 2 parameter
echo "You are passing 2 parameter";
break;
default:
throw new exception("Bad argument");
}
}
else
{
throw new exception("Function $method_name does not exists ");
}
}
}
$a = new test();
$a->overlodedFunction("ankur");
$a->overlodedFunction("techflirt" , "ankur");
As in above class test magic method __call is implemented which is managing overloading
Now in the __call function I have applied if condition to ensure that our business logic of overloading works
only for overlodedFunction function. In if block we have counted number of argument in parameter and
applied business logic.
Overriding in PHP
Overriding in php is very easy. As we know that overriding is process of modifying the inherited method. So
in case of inheritance you only need to create method with same name in your child class which you want to
override. Following is example of overriding of method in php.
class testParent
{
public function f1()
{
echo 1;
}
public function f2()
{
echo 2;
}
}
class testChild
{
function f2($a) //overriding function f2
{
echo "$a";
}
}
$a = new testChild();
$a->f2("ankur");//it will print ankur
In above example you are overriding function f2. While overriding you are free to change business
logic, visibility and number of parameter.
I hope your concept of the overloading and overriding in php is clear now.
For more detail about overloading and overriding in php you can read:
http://php.net/manual/en/language.oop5.overloading.php
http://en.wikipedia.org/wiki/Function_overloading
http://en.wikipedia.org/wiki/Method_overriding