Magic Methods
Some predefined function by php compiler which executes on some event is called magic methods. Magic methods starts with prefix __, for example __call, __get, __set. There are diffrent type of magic methods in php. List given below-
__construct : __construct method is called when we create object of our class. Basically this is used for creating constructor in php5.
__destruct : __destruct method is called when object of our class is unset. This is just opposite of __construct.
__get : __get is called when our object attempt to read property or variable of the class which is inaccessible or unavailable.
__set : __set is called when object of our class attempts to set value of the property which is really inaccessible or unavailable in our class.
__isset : __isset methods trigger when isset() function is applied on any property of the class which isinaccessible or unavailable.
__unset : __unset is something opposite of isset method. This method triggers when unset() function called on inaccessible or unavailable property of the class.
__call : __call magic method trigger when we are attempting to call method or function of the class which is either inaccessible or unavailable.
__callstatic : __callstatic execture when inaccessible or unavailable method is in static context.
__sleep : __sleep methods trigger when we are going to serialize your class object.
__wakeup : __wakeup executes when we are un serializing any class object.
__toString : __toString executes when you are using echo on your object.
__invoke : __invoke called when we are using object of your class as function
Above magic methods of php executes on some specif events occur on your class object. For example if you simply echo your object then __toString method trigger.
Let us create group of related magic method and analyze how it is working
__construct and __destruct magic method in PHP
__construct method trigger on creation of object. And __destruct triggers of deletion of object.
Simple example of __construct and __destruct magic method
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class test { function __construct() { echo ok; } function __destruct() { echo delete; } } $objT = new test(); //__construct get automatically executed and print ok unset($objT);//__destruct triggers and print delete. |
Basic example of __get , __set , __call and __callStatic magic methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class test { function __get($name) { echo "__get executed with name $name "; } function __set($name , $value) { echo "__set executed with name $name , value $value"; } function __call($name , $parameter) { $a = print_r($parameter , true); //taking recursive array in string echo "__call executed with name $name , parameter $a"; } static function __callStatic($name , $parameter) { $a = print_r($parameter , true); //taking recursive array in string echo "__callStatic executed with name $name , parameter $a"; } } $a = new test(); $a->abc = 3;//__set will executed $app = $a->pqr;//__get will triggerd $a->getMyName('ankur' , 'techflirt', 'etc');//__call willl be executed test::xyz('1' , 'qpc' , 'test');//__callstatic will be executed |
Basic example of __isset and __unset magic method in php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class test { function __isset($name) { echo "__isset is called for $name"; } function __unset($name) { echo "__unset is called for $name"; } } $a = new test(); isset($a->x); unset($a->c); |