__sleep() and __wakeup() magic method in oop

<?php

/*__sleep() magic method automatically called when an object of class is to be serialized .. it does not accept any parameter … it return array with element name of some properties name in the class … the properties names are those that you don’t wish to be serialized …

__wakeup() magic method automatically called when an object of class is to be unserialized .. it does not accept any parameter and do not return anything …*/

class myth
{
public $a;
public $b;
function set($a,$b)
{
$this->a = $a;
$this->b = $b;
}
function get()
{
echo $this->a;
}
function __sleep()
{
return array(‘b’);
}
function __wakeup()
{
echo “After the object is unserialized !!;<br>”;
}
}

$ob = new myth();
var_dump($ob);
echo “<br>”;
$ob->set(1,2);
var_dump($ob);
echo “<br>”;
$ob2 = serialize($ob);
var_dump($ob2);
echo “<br>”;
var_dump(unserialize($ob2));
echo “<br>”;
?>

/*Check out the output when the object is serialized …*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s