Magic Methods in Php – __sleep() and __wakeup()

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.

Example :


class Customers {
 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 Customers();
$c->setName("Hiren");
$c->setCC("1234567890123456");
 
$data = serialize($c)."\n";
echo $data."\n";
Output is :
O:8:”Customers”:1:{s:14:” Customers name”;s:5:”Hiren”;}
In the above example, you can see that the serialized string data only contains the name of the Customers 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.

Example :

class Customers {
 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 == "Hiren") {
   //you would ideally fetch CC data from Database
   $this->credit_card_number = "1234523490123456";
  }
 }
}
 
$c = new Customers();
$c->setName("Hiren");
$c->setCC("1234523490123456");
 
$data = serialize($c)."\n";
var_dump(unserialize($data));

Output:

object(Customers)#2 (2) {
[name:private]=>
string(5) "Hiren"
[credit_card_number:private]=>
string(16) "1234523490123456"
}
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.


No comments: