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.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments