Thursday 11 April 2019

Function Overriding and Function Overloading in PHP

Function Overriding

What is method overriding in PHP?
Two methods with same name and parameter is called overriding. In Function overriding, both parent class and child class having method with same name and arguments. Purpose of method overriding is to change the behaviour of parent class method.

How to implement overriding in PHP?

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();
  echo $obj->text(); // display the parent class method

  $obj= new parent_class();
  echo $obj->test();

  $obj= new child();
  echo $obj->text(); // display the child class method

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

Function Overloading

Function overloading allows to create method with same name but different input parameter.
Method overloading means to assign extra work to same method.

How to implement overloading in PHP?

<?php
  class text {
    public function display($parameter1) {
      echo "Hello world!!";
    }
    public function display($parameter1,$parameter2) {
      echo "Hello India!!”;
    }
  }

  $obj = new text;
  echo $obj->display('Hello'); // It will show fatal error

?>

Output: 

Fatal error: Cannot redeclare text::display() 

Above code will give fatal error. With the above code output, overloading with the same name function cannot be possible. With the help of magic function(__call()) overloading is done. __call() function takes function and arguments.

Example, find the area of certain shapes where radius are given then it should return area of circle if height and width are given then it should give area of rectangle and others.

<?php
  class Shape {
    const Pi = 3.142 ;  // constant value
    
    // __call is magic function which accepts  
    // function name and arguments
    function __call($func_name, $argument) {
      // It will match the function name
      if ($func_name == 'area') {
        switch (count($argument)) {
          // If there is no argument 
  // return 0
  // If there is only one argument 
          // area of circle 
  // If there is two arguments 
          // area of rectangle 
          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 Shape();
  echo "Area of circle:".$circle->area(5)."</br>"; // display the area of circle
  $rect = new Shape();
  echo "Area of rectangle:".$rect->area(5,10); // display area of rectangle
?>

__call() method automatically called behind the scene.

No comments:

Post a Comment