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.

What is Method Overriding in OOP ?

Basic meaning of overriding in oop is same as real word meaning. In real word meaning of overriding  phenomena of replacing the same parental behavior in child. This is same in case of method overriding in oop. In oop meaning of overriding is to replace parent class method in child class. Or in simple technical word method overriding mean changing behavior of the method. In oop overriding is process by which you can re-declare your parent class method in child class. So basic meaning of overriding in oop is to change behavior of your parent class method.

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.

What is Method Overloading in OOP ?

Overloading in oop is same as overloading in real word. In real word overloading means assigning extra work to same machine or person. In oop method overloading is same. By process of method overloading you are asking your method to some extra work. Or in some cases we can say some different work also.

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.

Overloading and Overriding in PHP

Hope your basic concept of overloading and overriding is clear now. Now let us explore implementation of overloading and overriding in php.

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

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 ");
}
}

As we know that __call magic method invoked when method is not available in the class. So in case of above test class example we have not created function overlodedFunction. So whenever method overlodedFunction is called __call invoked. __call pass 2 variable, first  name of the called method and other is parameter passed in the called function.

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

Next >> Object Cloning In PHP

Tagged with:
 

22 Responses to Overloading and Overriding in PHP

  1. free204 says:

    Thanks, you have forget “extends” word in second example “Overriding in PHP” with this you have nothing to override. :)

  2. Your theory part is excellent but you forget extends key word in overriding example program

  3. class testParent
    {
    public function f1()
    {
    echo 1;
    }
    public function f2()
    {
    echo 2;
    }
    }
    class testChild extends testParent
    {
    function f2($a) //overriding function f2
    {
    echo “$a”;
    }
    }
    $a = new testChild();
    $a->f2(“ankur”);//it will print ankur

    this is right program

  4. [email protected] says:

    Good!!!!!!!!!!

    Easy to understand and implement.

    Thanx!!!!!!

  5. sarvesh says:

    This is really awesome stuff

  6. hemant ram says:

    You had clear my concept using very simple and easy to understand examples.

  7. aman kamboj says:

    Great work….

  8. saman says:

    Grea8 thanks

  9. in your override concept example, you have created different signature in child class for f2() method, which is not allowed in php it causes following “strict standers”

    Strict Standards: Declaration of testChild::f2() should be compatible with that of testParent::f2()

    solution – The method which you have overridden should have same number of parameters as parent class.

  10. Jeet Patel says:

    Its very helpful to learn about advanced OOPs.

    Thanks ankur

  11. Navneet Garg says:

    you can not change signature in overriding

  12. Navneet Garg says:

    you can not change signature in overriding. this code is not showing error because you off display error in php.ini

  13. sugyan kumar sahoo says:

    can any one put a real life example here please?

  14. [email protected] says:

    yyyuuupppeeeee easy to implemennt

  15. Good effort and better explanation of overloading/overing concept of PHP……except code example :)

  16. Jmg says:

    This tutorial was very helpful.

  17. Jatin Seth says:

    Well written…

  18. I found it very useful to understand these concepts. Thanks.

Leave a Reply