PHP Overloading vs Overriding

Overloading:

Types of Overloading in PHP:

Property Overloading

All overloading methods must be defined as Public.

  • __set(): triggered while initializing overloaded properties.
  • __get(): triggered while using overloaded properties with PHP print statements.
  • __isset(): This magic method is invoked when we check overloaded properties with isset() function
  • __unset(): Similarly, this function will be invoked on using PHP unset() for overloaded properties.

class MyClass
{
    /**  Array for overloaded data.  */
    private $data = array();

    public function __set($name, $value)
    {
        $this->data[$name] = $value;
    }
    
    public function __get($name)
    {
        return $this->data[$name];
    }

    public function __isset($name)
    {
        return isset($this->data[$name]);
    }

    public function __unset($name)
    {
        unset($this->data[$name]);
    }
}
$obj = new MyClass;
$obj->name = "John Doe";
echo $obj->name; // John Doe

Method Overloading

Function overloading contains same function name and that function preforms different task according to number of arguments. For 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. Like other OOP languages function overloading can not be done by native approach. In PHP function overloading is done with the help of magic function __call(). This function takes function name and arguments. Most of the magic methods will be triggered in object context except __callStatic() method which is used in a static context.

  • __call() – triggered while invoking overloaded methods in the object context.
  • __callStatic() – triggered while invoking overloaded methods in static context.

class MyClass { 

    public function __call($name, $arguments) { 
          
        echo "Calling object method '$name' "
            . implode(', ', $arguments). "\n"; 
    } 
  
      
    public static function __callStatic($name, $arguments) { 
          
        echo "Calling static method '$name' "
            . implode(', ', $arguments). "\n"; 
    } 
} 
      

$obj = new MyClass; 

$obj->myMethod('in object context'); // Calling object method 'runTest' in object context
  
MyClass::myMethod('in static context');  // Calling static method 'runTest' in static context

Overriding:

In function overriding, both parent and child classes will have same function name and number of arguments. It is used to replace parent method in child class. The purpose of overriding is to change the behavior of parent class method. Creating two methods with the same name and same parameters in inherited classes is called overriding.


class ParentClass { 
      
    function process(array $data):array
	{ 
        $array = array_filter($data, function($v){
	    return $v < 3;
	}); 
	return $array;
    } 
} 
   
class ChildClass extends ParentClass { 
      
    function process(array $data):array
    { 
        return array_map(function($v){
	    return $v * 2;
        }, $data); 
    }  
}
$parent = new ParentClass();
$child = new ChildClass();
$filtered = $parent->process(range(1,5));
$doubled = $child->process(range(1,5));
echo '
';
var_dump($filtered, $doubled);
/*
Results

array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
array(5) {
  [0]=>
  int(2)
  [1]=>
  int(4)
  [2]=>
  int(6)
  [3]=>
  int(8)
  [4]=>
  int(10)
}
*/