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

Popular posts from this blog

GROUP BY Clause and HAVING Clause in PHP

Method Overriding in Traits in PHP

Mysqli database Connection and Display Table Data from Database