PHP 5 Tutorial - __wakeup() Magic Method

This tutorial will guide you through the __wakeup() Magic Method. The __wakeup() magic method of PHP5 gets called when unserialize operation on object is performed. This method allows us to restore the serialized data to its normal form.





Syntax:

function __wakeup()
{
	...
}

Example - __wakeup magic method Call

< ?
class magicmethod
{
	private $setName;
	function __sleep()
	{
		echo "Performing Clean-Up Operation Before Serializing Data ";
		$this->setName = "Hello World!!!";
		return array(setName);
	}
 
	function __wakeup()
	{
		echo "Performing Clean-Up Operation Before Unserializing Data ";
		echo $this->setName;
	}
}
$a = new magicmethod();
$serializedata = serialize($a);
$serializedata1 = unserialize($serializedata);
?>

Output:
Performing Clean-Up Operation Before Serializing Data
Performing Clean-Up Operation Before Unserializing Data
Hello World!!!
Explanation for the Example:

Here i am trying to serialize the object of magicmethod class

Now the PHP Compiler calls the __sleep method which return an array having the serialized values

After serialize data, i am calling the unserialize function; now the PHP compiler will call the __wakeup method which contains the original data that was serialized

Similar Posts

Custom Search

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)