Sleep Method
<?php
//serialize() function is used to Convert any Object into Array.
//__sleep() function will be executed before serialize().
//When we serialize any Object at that time __sleep() Magic function will automatically Call
//before serialize().
//serialize() function will convert Object to Array. We pass Object to serialize() function.
//unserialize() function will convert Array to Object. We pass Array to unserialize() function.
//When we use serialize() function at that time __sleep() method will automatically Call.
//When we use unserialize() funtion at that time __wakeup() method will automatically Call.
class Student
{
public $course = "PHP";
private $first_name;
private $last_name;
public function setName($fname, $lname)
{
$this->first_name = $fname;
$this->last_name = $lname;
}
public function __sleep()
{
return array("first_name","last_name");
}
}
$obj = new Student();
$obj->setName("Yahoo","Baba");
$srl = serialize($obj);
echo $srl;
// OUTPUT:
//O:7:"Student":2:{s:19:"Studentfirst_name";s:5:"Yahoo";s:18:"Studentlast_name";s:4:"Baba";}
?>
Comments
Post a Comment