Open In App

What is difference between __sleep and __wakeup() in PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

In PHP, the __sleep and the __wakeup methods are called as magic methods. These methods are invoked or executed when we want to deal with serialization and deserialization of objects during runtime to store or save the object information in string format and again the information can be restored. 

__sleep() Method: This method is used for serialization. Serialization is used to save or store the information of the object in the form of string representation in a database, cookies, servers, sessions, files, etc. This method returns an array that carries the properties of an object, included in the serialized version of that particular object. This method is generally invoked before initiating the serializing of the object, which tells what properties to be included in the string representation. In other words, the__sleep() method provides a way for saving the information of the class instances (i.e., variables, methods, etc.,) in the string format in a file or in any other mentioned location or path. 

Syntax:

public function __sleep() {
    return statement (object)
}

Example 1: In this example, we declare the array and initialize the values in the __construct() method. In __sleep() method, return the array values and it will simply store the object information in the form of a string. To call this __sleep() method, create an object for the class and serialize the object in order to call the __sleep() method. It will simply display the array information in the form of a string in the output. 

PHP




<?php
  class Sleep_Wakeup {
    public $arrayM;
    public function __construct() {
        $this->arrayM = array(1, 2, 3, 4, 5);
    }
 
    public function __sleep() {
        echo "I am from __sleep() method \n";
        return array('arrayM');
    }
}
  $obj = new Sleep_Wakeup();
  $serializedStr = serialize($obj); //serializing the object
  var_dump($serializedStr); //printing the serialized object
?>


Output:

I am from __sleep() method 
string(83) "O:12:"Sleep_Wakeup":1:{s:6:"arrayM";a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;}}"

__wakeup() Method: This method is used for deserialization. Deserialization is used to restore the saved string representation to its original form. De-Serialization is a vice-versa of Serialization.  __wakeup() method is also used to provide initialization or to call the various resources such as database, file, server, etc.,

Syntax:

public function __wakeup() {
    // Initializing the connection
    // to resources (file, database etc.,)
}

Example 2: In this example, declare the resource variable. In the __wakeup() method, provide the initialization to open a file and __wakeup() will open the file. To call this __wakeup() method, create an object for the class and serialize the object first before unserializing. Now, unserialize the object to call the __wakeup() method. It will simply call the file or other sources depending on the initialization in the __wakeup() method.

PHP




<?php
     
  class Sleep_Wakeup {     
    public $resource; //declaration
    
    public function __wakeup() {
        echo "I am from __wakeup() method \n";
 
        // Opening the file
        $this->resource = fopen("demo.txt", "w");
    }
}
 
$obj = new Sleep_Wakeup();
 
// Serializing the object for string representation
$serializedStr = serialize($obj);
 
// Unserializing the string object
var_dump(unserialize($serializedStr));
?>


Output 

I am from __wakeup() method 
object(Sleep_Wakeup)#2 (1) {
  ["resource"]=>
  resource(5) of type (stream)
}

Difference between the  __sleep() Method and __wakeup() Method:

S.No

                         __sleep() Method

                            __wakeup() Method

  1

It is used when we want to serialize an object.

It is called when we want to de-serialize an object

  2

The serialize() method will always check whether the program contains __sleep method or not before execution.

The unserialize() method will always check whether the program contains __wakeup method or not before execution.

  3

It is used in cleaning up pending tasks.

It is used in re-establishing the database or any other connection that has been lost during the serialization process.

  4

It simply cleans the object and returns an array with all the variables present in that class instance which is serialized. 

It simply re-creates or re-constructs the object which we want to de-serialize.

  5

If __sleep presents in the code then the serialize() will always invoke the __sleep method first.

If __wakeup presents in the code then the unserialize() will always invoke the __wakeup method first.



Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads