Overloading in PHP

October 1, 2013

We are going to see a surprise in this PHP tutorial. We all know about overloading and it is a basic concept in OOPS and we have beaten it enough in our colleges. We have operator overloading, function overloading etc. When different operations are performed using the same operator, then it is called operator overloading. Similarly, if we define functions having identical name but different set of arguments, then it is called function overloading.

In PHP we have overloading. Lot of people say different things, like we do not have overloading in PHP or there is only partial support for overloading in PHP and what PHP does is not overloading. So what is my take? I am going to go by PHP bible, the php.net manual. Following is what the PHP manual says about overloading,

PHP’s interpretation of “overloading” is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.

This is kind or tricky, it says PHP’s interpretation. There is for sure something muddy here. I would say, let us not worry about whether it is the traditional OOPS overloading or not and just skip to PHP’s 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.

Dynamic Entities on PHP Overloading

Properties and methods are those entities created dynamically by using PHP overloading. After creating object for a class, we can access set of entities, that are, properties or methods not defined within the scope of the class. Such entities are said to be overloaded properties or methods, and the process is called as overloading.

For working with these overloaded properties or functions, PHP magic methods are used. Most of the magic methods will be triggered in object context except __callStatic() method which is used in static context.

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 allows us to create dynamic properties in object context. For creating those properties no separate line of code is needed. A property associated with class instance, and it is not declared within the scope of the class, is considered as overloaded property.

We can perform the following operations with overloaded properties in PHP.

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

Before performing the above three operations, we should define appropriate magic methods. Those methods are,

  • __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.

Example: PHP Property Overloading

This PHP example is to create property array elements dynamically.

<?php
class Toys{
private $str;
public function __set($name,$value){
$this->str[$name] = $value;
}
public function __get($name){
echo "Overloaded Property name = " . $this->str[$name] . "<br/>";
}
public function __isset($name){
if(isset($this->str[$name])){
echo "Property \$$name is set.<br/>";		
} else {
echo "Property \$$name is not set.<br/>";
}
}
public function __unset($name){
unset($this->str[$name]);
echo "\$$name is unset <br/>";
}
}
$objToys = new Toys;
/* setters and getters on dynamic properties */
$objToys->overloaded_property = "new";
echo $objToys->overloaded_property . "\n\n";
/*Operations with dynamic properties values*/
isset($objToys->overloaded_property);
unset($objToys->overloaded_property);
isset($objToys->overloaded_property);
?>

In the above example PHP program, 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 $str.

Added to that, isset() and unset() with overloaded property will trigger __isset() and __unset() magic methods. After unset(), if we call isset(), then it will print “property not set” message to the browser.

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 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 object context.
  • __callStatic() – triggered while invoking overloaded methods in static context.

PHP example for Method Overloading

Let us call undefined class function with both object reference and with 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 Toys
{
public function __call($name,$param){
echo "Magic method invoked while method overloading with object reference<br/>";
}
public static function __callStatic($name,$param){
echo "Magic method invoked while method overloading with static access<br/>";
}
}
$objToys = new Toys;
$objToys->overloaded_method();
Toys::overloaded_property();
?>

Download Overloading in PHP Source Code

«

»

Add a Comment