articles

Home / DeveloperSection / Articles / Overloading and overriding in PHP

Overloading and overriding in PHP

Anonymous User 28291 20-Jun-2016

Overloading and overriding method is very useful feature of any object oriented programming language. In this section we will discuss how to implement method overloading and overriding in PHP. In object oriented programming concept if methods of the class has the same name but different in parameters are termed as overloading and if the methods of the class are same as well as parameter then it is termed as overriding

What is method overloading in PHP?

Function overloading is a feature of object oriented programming that allows creating methods with the same name but differ in the type of input parameter. In other words, we can say that functions are used to perform different tasks. Overloading in object oriented programming is same as real word overloading. In real world, method overloading means to assign extra work to same person. In programming language, you are asking method to do some extra work or some different work.

Overloading in PHP creates properties and methods dynamically. These dynamic properties and method are processes using magic method.

How to implement overloading in PHP?

Following is an example which shows how two function with same name works in PHP:

<?php
class text {
  public function display($parameter1) {
  echo "Hello world!!";
 }
public function display($parameter1,$parameter2) {
echo "Hello India!!”;
  }}
$obj = new text;
$obj->display('Hello'); // It will show fatal error
?>

  Output: 

Fatal error: Cannot redeclare text::display() 

As from above example we can say that in PHP overloading with same name function can’t be possible. Therefore, with the help of magic function overloading is done in PHP.

Following is an example of overloading with the help of magic methods:

<?php
class TDshape {
const Pi = 3.142 ; // constant value
 function __call($fname, $argument){     if($name == 'area')
        switch(count($argument)){             case 0 : return 0 ;
            case 1 : return self::Pi * $argument[0] ; // 3.14 * 5
            case 2 : return $argument[0] * $argument[1]; // 5 * 10
        }
    }
}
$circle = new TDshape();
echo "Area of circle:".$circle->area(5)."</br>"; // display the area of circle
 $rect = new TDshape();
echo "Area of rectangle:".$rect->area(5,10); // display area of rectangle
?>

  Output 

Area of circle:15.71

Area of rectangle:50

 

In the above example __call is a magic method. This method is automatically called behind the scene.

What is method overriding in PHP?

In object oriented programming overriding is to replace parent method in child class.In overriding you can re-declare parent class method in child class. So, basically the purpose of overriding is to change the behavior of your parent class method.

When your class  has some method and another class(derived class) want the same method with different behavior then using overriding you can completely change the behavior of base class(or parent class). The two methods with the same name and same parameter is called overriding.

How to implement overriding in PHP?

Overriding concept is very easy in PHP. As we all know overriding in PHP is a method of updating the inherited method from parent class to child class. So, in this case you will require method with same name in parent class as well as in child class and then we will see how the behavior of parent class method is changed when child class override the method of parent class.

Following is an example of overriding in PHP:
<?php
 class parent_class
 {
 public function text() //text() is a parent class method
 {
echo "Hello!! everyone I am parent class text method"."</br>";
 }
public function test()
 {
 echo "Hello!! I am second method of parent class"."</br>";
 }
 }
 class child extends parent_class
 {
 public function text() // Text() parent class method which is override by child class
 {
 echo "Hello!! Everyone i am child class";
 }
 }
 $obj= new parent_class();
 $obj->text(); // display the parent class method echo
$obj= new parent_class();
 $obj->test();
 $obj= new child();
 $obj->text(); // display the child class method echo
?>

Output: 

Hello!! Everyone I am parent class text method

Hello!! I am second method of parent class
Hello!! everyone i am child class

As from the above example we can see how text() method of parent class is overridden by child class.


php php 
Updated 10-Jun-2020
I am a content writter !

Leave Comment

Comments

Liked By