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 an identical name but different set of arguments, then it is called function overloading.
In PHP we have to overload. A lot of people say different things like we do not have to overload 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 is to create dynamic entities. In this tutorial, we will understand about those dynamic entities, how they are created and how to access them.
Properties and methods are those entities created dynamically by using PHP overloading. After creating an 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 can be classified as,
PHP property overloading allows us to create dynamic properties in the object context. For creating those properties no separate line of code is needed. A property associated with the 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.
Before performing the above three operations, we should define appropriate magic methods. Those methods are,
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, 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 $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.
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,
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 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
this code is very helpful me
Thank you Kevin.
Very helpful!
Thank You !!!
Welcome Bharat.