Explain the Magic Method in Detail
The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() and __clone() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them
/** class to test magic method i.e sleep and wakeup **/
class magicmethod
{
public $name1 = 'Hello';
function __wakeup() {
echo "__wakeup funx called <br />";
echo $this->name1;
}
function __sleep(){
echo "__sleep funx called <br />";
$this->name1 ='Arun kumar';
return array('name1');
}
}
$obj = new magicmethod();
$objString = serialize($obj);
echo $objString;
echo "<br>" ;
unserialize($objString);?>
//output
__sleep funx called
O:11:"magicmethod":1:{s:5:"name1";s:10:"Arun kumar";}
__wakeup funx called
Arun kumar
Other Exmples : click
0 Comment:
Post a Comment