Wakeup Method

<?php
//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.
//__wakeup() is mostly used while making connection with database operations.

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");
    }

    public function __wakeup()
    {
        echo "This is a Wake up Method";
    }

}
$object = new Student();
$object->setName("Hello","World");

$srl = serialize($object);

$unsrl = unserialize($srl);
echo "<pre>";
print_r($unsrl);
echo "</pre>";

//  OUTPUT:
//This is a Wake up Method
// Student Object
// (
//     [course] => PHP
//     [first_name:Student:private] => Hello
//     [last_name:Student:private] => World
// )

?>

Comments

Popular posts from this blog

Ajax Insert Records in Database Tutorial in PHP

Displaying Database data using Class, Object, Constructor, Destructor in PHP MySql

SubQuery with EXISTS and NOT EXISTS in PHP