Home > PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials > PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()

PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()

November 3rd, 2007 admin Leave a comment Go to comments

The magic method __sleep() and __wakeup() is called when an object is serialized. The magic method __sleep() and __wakeup() provides a method to clean up and restore objects before being serialized.

Working with the magic method __sleep()

__sleep() magic method is called when the object of a class is about to be serialized. This magic method __sleep() does not accept any parameter and returns an array. The array should contain a list of class members that should be serialized. This means that if you don’t wish to serialize a particular class member, you should not include it in the array. Look at the example below:




class Customer {
	private $name;
	private $credit_card_number;
 
	public function setName($name) {
		$this->name = $name;
	}
 
	public function getName() {
		return $this->name;
	}
 
	public function setCC($cc) {
		$this->credit_card_number = $cc;
	}
 
	public function getCC() {
		return $this->credit_card_number;
	}
 
	public function __sleep() {
		return array("name"); //because of this, only name is serialized
	}
 
}
 
$c = new Customer();
$c->setName("Sunil");
$c->setCC("1234567890123456");
 
$data = serialize($c)."\n";
echo $data."\n";

Output:
O:8:”Customer”:1:{s:14:” Customer name”;s:5:”Sunil”;}

In the above example, you can see that the serialized string data only contains the name of the Customer Object. This is because the __sleep() magic method returned an array containing only the ‘name’ data member.




Working with the magic method __wakeup()

__wakeup() magic method is the opposite of the __sleep() method. It is called when the object of a class is about to be unserialized. This magic method __wakeup() does not accept any parameter nor returns anything. The __wakeup() method is responsible for setup operations after an object has been unserialized. Look at the example below:

class Customer {
	private $name;
	private $credit_card_number;
 
	public function setName($name) {
		$this->name = $name;
	}
 
	public function getName() {
		return $this->name;
	}
 
	public function setCC($cc) {
		$this->credit_card_number = $cc;
	}
 
	public function getCC() {
		return $this->credit_card_number;
	}
 
	public function __sleep() {
		return array("name");
	}
 
	public function __wakeup() {
		if($this->name == "Sunil") {
			//you would ideally fetch CC data from Database
			$this->credit_card_number = "1234567890123456";
		}
	}
}
 
$c = new Customer();
$c->setName("Sunil");
$c->setCC("1234567890123456");
 
$data = serialize($c)."\n";
var_dump(unserialize($data));

Output:
object(Customer)#2 (2) {
[”name:private”]=>
string(5) “Sunil”
[”credit_card_number:private”]=>
string(16) “1234567890123456″
}

In the above example, you can see that after the $c object has been serialized and the output stored in $data variable, we use the $data variable and pass it to the unserialize(). Before the object is unserizlied and object created, the __wakeup() method is called. In the __wakeup() method you should ideally make a database call to fetch data of the missing member variable.

Please feel free to leave behind any comments or questions that you might have.

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Deepa
    November 5th, 2009 at 17:13 | #1

    I went through the all PHP Tutorial articles. Thanks for providing this informative articles.

  1. November 5th, 2007 at 22:02 | #1
  2. November 8th, 2007 at 23:01 | #2
Enter this code to leave comment (Sorry, but bots get me crazy :) )