PHP oops - Method Overloading

What is Overloading in PHP?

If method have same name and different quantities and types of parameters, then it is called as Method Overloading in most object oriented languages. But Magic methods are used to achieve method overloading in case of php.

PHP Example:

<?php

//code by aryatechno for Method Overloading
   class Maths {
      function sum($num1){
         return $num1;
      }
      function sum($num1,$num2){
         return $num1 + $num2 ;
      }
   }
   $result = new Maths();
   $result->sum(5,10);
?>

Output:

PHP Fatal error:  Cannot redeclare Maths::sum()

Explaination: As per as above example , We can see that same method name with different number and type of arguments can not be redeclared in same class using php except most other object oriented languages. So Method Overloading can be done using magic methods in php.

Types of Overloading in PHP

There are two types of overloading in PHP as per as below.

  1. Method Overloading
  2. Property Overloading

Method overloading in PHP

  • All overloading methods must be defined as public.
  • PHP magic methods are used to get methods overloading in php
  • Function or Method overloading is done using magic function __call() and __callStatic in php

PHP Syntax:

public __call ( string $name , array $arguments ) 

__call() is triggered when trying to access inaccessible methods in an object context.

public static __callStatic ( string $name , array $arguments )

__callStatic() is triggered when trying to access inaccessible methods in a static context.

The $name argument is the name of the method being called.
The $arguments argument is array containing the parameters passed to the $name argument.

PHP Code Example:

<?php
   //code by aryatechno for Method Overloading
   class Aryatechno {
     
      function __call($name,$arg){
         
         echo "<br/> Magic method __call is triggered when trying to accees non-existing method course()";
         echo "<br/> Method Name :".$name;
         echo "<br/> Passes Argument in array:".$arg[0];
   }
}
   $obj = new Aryatechno();
   $obj->course("php");
?>

Output:
Magic method __call is triggered when trying to accees non-existing method course()
Method Name :course
Passes Argument in array:php

Property Overloading in PHP

  • PHP property overloading is used to create dynamic properties in the object context.
  • PHP property overloading is done using __get(), __set(), __isset() and __unset() magic function.

PHP Syntax:

public __set ( string $name , mixed $value )

public __get ( string $name )

public __isset ( string $name )

public __unset ( string $name )

The $name argument is the name of the property and $value is value of $name argument.

__set() is triggered when writing data to inaccessible (like protected or private) or non-existing properties.

__get() is called  when reading data from inaccessible (like  protected or private) or non-existing properties.

__isset() is triggered when isset() or empty() is called on inaccessible (like protected or private) or non-existing properties.

__unset() is triggered when unset() is called on inaccessible (like protected or private) or non-existing properties.

PHP Code Example:

<?php
//code by aryatechno for Method Overloading

class Aryatechno
{
    function __set($name,$val)
    {
        echo "<br/> __set magic method is called when writing value into inaccessiable property";    
        echo "<br/> Inaccessiable variable Name :".$name;
        echo "<br/> variable value :".$val;
        
    }
    
    function __get($name)
    {
        
        echo "<br/> __get magic method is called when reading inaccessiable property";    
        echo "<br/> Inaccessiable variable Name :".$name;
        
    }
    
    function __isset($name)
    {
        echo "<br/> __isset magic method is called when isset() method is called for inaccessiable property";    
        echo "<br/> variable Name :".$name;
    }
    
    function __unset($name)
    {
        echo "<br/> __unset magic method is called when unset() method is called for inaccessiable property";    
        echo "<br/> Inaccessiable variable Name :".$name;
    }
    
    
}

$obj = new Aryatechno;
$obj->i=100; //variable i is not available

echo $obj->j; //variable j is not available
isset($obj->p); //variable p is not available
unset($obj->p);

?>

Output:

__set magic method is called when writing value into inaccessiable property
Inaccessiable variable Name :i
variable value :100
__get magic method is called when reading inaccessiable property
Inaccessiable variable Name :j
__isset magic method is called when isset() method is called for inaccessiable property
variable Name :p
__unset magic method is called when unset() method is called for inaccessiable property
Inaccessiable variable Name :p

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

24919