Sleeping and waking up in PHP

Nov 30, 2010 Author: Dusan

Sleeping and waking up in PHP PHP provides a hook mechanism so that objects can specify what should happen just before serialization and just after unserialization. The special member function __sleep() (that’s two underscores before the word sleep), if defined in an object that is being serialized, will be called automatically at serialization time. It is also required to return an array of the names of variables whose values are to be serialized.

This offers a way to not bother serializing member variables that are not expected to survive serialization anyway (such as database resources) or that are expensive to store and can be easily recreated. The special function __wakeup() (again, two underscores) is the flip side — it is called at unserialization time (if defined in the class) and is likely to do the inverse of whatever is done by __sleep() (restore database connections that were dropped by __sleep() or recreate variables that __sleep() said not to bother with).

You may wonder why these functions are necessary — couldn’t the code that calls serialize() also just do whatever is necessary to shut down the object? Actually, it’s very much in keeping with OOP to include such knowledge in the class definition rather than expecting the code using the objects to know about their special needs. Also the calling code may have no knowledge of the object’s internals at all (as in the code that serializes all session objects).

The author of the class is uniquely qualified to say what should happen when an instance is sent away or revived. As an example of how to use these functions, here is the previous serialization example, augmented with an extra variable, and the __sleep() and __wakeup() functions:

class ClassToSerialize2 {
public $storedStatement = “data”;
public $easilyRecreatable = “data again”;
public function __construct($statement) {
$this->storedStatement = $statement;
$this->easilyRecreatable =
$this->storedStatement . “ Again!”;
public function __sleep() {
// Could include DB cleanup code here
return array(‘storedStatement’);
public function __wakeup() {
// Could include DB restoration code here
$this->easilyRecreatable =
$this->storedStatement . “ Again!”;
public function display ()
print($this->easilyRecreatable . “
”); } } $instance1 = new ClassToSerialize2(“You’re objectifying me!”); $serialization = serialize($instance1); $instance2 = unserialize($serialization); $instance2->display();

The variable called $easilyRecreatable is meant to stand in for a piece of data that is (1) expensive to store and (2) implied by the other data in the class anyway. The definition of __sleep() does no cleanup itself, but it returns an array that contains only one variable name and does not include easilyRecreatable.

At serialization time, only the value of the variable storedStatement is included in the string. When the object is recreated, the __wakeup() function assigns a value into $this->easilyRecreatable, which is then displayed: You’re objectifying me! Again!

views 4657
  1. Add New Comment