Property Overloading in PHP

Overloading in PHP handled in different manner which is not similar to other programming language.With PHP overloading you can dynamically create properties and methods.These entities are processed by introducing some special methods which are known as “Magic Methods”. These magic methods are known as overloading methods in PHP. In PHP overloading done on different level for example:-

i) Property Overloading
ii) Object Overloading
iii) Method Overloading

Property Overloading: As stated above property overloading can be achieved by magic methods.These magic methods are started with __functionName.

__set() :- This method automatically gets called when we try to assign any value to inaccesible property.

__get() :- This method automatically gets called when we try to get data of any inaccessble property.

__isset() :- This method automatically gets called once we call either empty() or isset() on inaccessible property.

__unset() :- This function automatically called when we call unset() on inaccessible property.

Here one thing is notable that property overloading can’t be declared as static.
Example of property overloading :

<?php
class PropertyOverloading {

/* Property overloading class start- */

private $_data = array ();

public function __set($name, $value) {

/*

* If someone will assign any value to inaccessible variable
* within class this function will get called automatically

*
* */
echo "<br>calling __set magic method <br>";
$this->_data [$name] = $value;

}

public function __get($name) {

/*
when we will try to retrive any value that is set through set()
* this function will get called automatically
*
* */

return $this->_data [$name];

}

public function __isset($name) {

/*

* If someone will call isset function
* then this magic method will get called

* */
echo "<br>calling __isset magic method<br>";

return isset ( $this->_data [$name] );

}

public function __unset($name) {

/*

* this will get called on calling unset function

* */

unset ( $this->_data [$name] );

}
}

$property_overloading = new PropertyOverloading ();

echo 'Property overloading starts now';

echo "<br /><br />";

var_dump ( isset ( $property_overloading->newProperty ) );

$property_overloading->newProperty = "new property added";

echo "<br />";

echo $property_overloading->newProperty;

echo "<br />";

var_dump ( isset ( $property_overloading->newProperty ) );

unset ( $property_overloading->newProperty );

echo "<br />";

var_dump ( isset ( $property_overloading->newProperty ) );

echo "<br /><br />";

echo 'Property overloading end now';

echo "<br /><br />";

You can see the example of above code below

Property overloading starts now

calling __isset magic method
bool(false)
calling __set magic method

new property added

calling __isset magic method
bool(true)

calling __isset magic method
bool(false)

Property overloading end now

Hope you have enjoyed it . In the next blog you can see Method overriding.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...