PHP’s Overloading Example

Faysal Ahmed
Oceanize Lab Geeks
Published in
3 min readDec 1, 2017
PHP Overloading

PHP’s Overloading

PHP’s overloading is to create dynamic entities. In this tutorial, we will understand about those dynamic entities, how they are created and how to access them.

As we realize that we can not implement overloading by make 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 protest isn’t accessible in class. So here we won’t make method precisely and will take help of __call method. Presently call method will give us 2 contention, first name of the method called and parameter of the function. Presently with the assistance of either switch case or if else we will implement overloading in php. Following is extremely basic case of overloading in php.

PHP’s Overloading Types

Overloading in PHP can be classified as,

  1. Property Overloading
  2. Method Overloading

Property Overloading

PHP property overloading enables us to make dynamic properties in the object setting. For making those properties no different line of code is required. A property related with the class instance, and it isn’t declared inside the extent of the class, is considered as overloaded property.

In below we will learn about some uses of overloaded properties in PHP.

  • Setting and getting overloaded properties.
  • Evaluating overloaded properties setting.
  • Undo such properties setting.

PHP property overloading use some magic methods. Which are described below.

__set() is run when writing data to inaccessible properties.

__get() is utilized for reading data from inaccessible properties.

__isset() is triggered by calling isset() or empty() on inaccessible properties.

__unset() is invoked when unset() is used on inaccessible properties.

class PropertyOverload
{
/** Location 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 PropertyOverload;
$obj->name = "Faysal Ahmed";
echo $obj->name; // Faysal Ahmed

In the above example PHP program, the dynamic property is created. While initializing this dynamic property, __set() is invoked with name and value pair to be initialized as the name and value of class property array element $data.

Added to that, isset() and unset() with overloaded property will trigger __isset() and __unset() magic methods.

Method Overloading:

This type of overloading is for creating dynamic methods that are not declared within the class scope. PHP method overloading also triggers magic methods dedicated for the appropriate purpose.

Unlike property overloading, PHP method overloading allows function call in both object and static context. The related magic functions are,

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

Let us call the undefined class function with both object reference and with the class name itself. For accessing function from outside class with the name of the class itself, it should be a static member of that class. So accessing some overloaded method with the name of the class will trigger static magic member defined within the class. For example

<?php
class MethodOverloading
{
public function __call($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
public static function __callStatic($name, $arguments)
{
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodOverloading;
$obj->runTest('in object context');
MethodTest::runTest('in static context');

--

--