[ Team LiB ] Previous Section Next Section

Storing and Retrieving Objects

Usually, you separate your objects from data storage. In other words, you use saved data to construct objects, and then when you are done, you store the data again. Occasionally, however, you want your object and data to persist intact. PHP provides two functions to help you.

To "freeze-dry" an object, you should pass it to the serialize() function. serialize() produces a string that you can then store in a file or a database or transmit to another script:


class apple {
  var $flavor="sweet";
}
$app = new apple();
$stored = serialize( $app );
print $stored;
// prints "0:5:"apple":1:{s:6:"flavor";s:5:"sweet";}"

You can convert the string produced by serialize() back into an object with the unserialize() function. If the original class is present at the time unserialize() is called, an exact copy of the original object is produced:


$new_app = unserialize( $stored );
print $new_app->flavor;
// prints "sweet"

In some circumstances, you need your objects to clean up a little before storage. This cleanup is particularly important if an object has a database connection open or is working with a file. By the same token, you might want your object to perform some sort of initialization when it is woken up. You can handle these needs by including two special methods in any object that might need to be serialized.

The __sleep() method is automatically called by serialize() before it packs up the object. This process allows you to perform any cleanup operations you might need. For the serialization to work, your __sleep() method must return an array of the property names that you want to be saved in the serialized string:


class apple {
  var $flavor="sweet";
  var $frozen = 0;
  function ___sleep( ) {
    $this->frozen++;
    // any clean up stuff goes here
    return array_keys( get_object_vars( $this) );
  }
}
$app = new apple ( );
$stored = serialize( $app );
print $stored;
// prints "0:5:"apple":2:{s:6:"flavor";s:5:"sweet";s:6:"frozen";i:1;}"

Notice the trick we used at the end of the __sleep() method to list the names of all the properties in the object. We used the built-in function get_object_vars(). This function requires an object and returns an associative array of all the properties belonging to it. We pass the result of our call to get_object_vars() to the array_keys() function. array_keys() accepts an array (usually an associative array) and returns an array of its keys.

PHP also supports a special method called __wakeup(). If it is defined, it is automatically called by unserialize(). This process enables you to resume database connections or to provide any other initialization the object might need. We might add the following method to our apple class:


function __wakeup( ) {
  print "This apple has been frozen ".$this->frozen." time(s)";
  // any initialization stuff goes here
}

Now that we have added __wakeup(), we can call unserialize();


$new_app = unserialize( $stored );
// prints "This apple has been frozen 1 time(s)"


    [ Team LiB ] Previous Section Next Section