Magic Methods

__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
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
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
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);
]]>

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.