Home arrow PHP arrow Page 4 - Using the Sleep and Wakeup Functions to Serialize Objects in PHP

Creating persisting objects - PHP

Curious about how to serialize your objects inside your beautifully-crafted application? Look no further. Welcome to the second part of the series "Serializing objects in PHP." This set of three tutorials introduces the fundamentals of objects serialization, and teaches you some advanced concepts regarding this approach, such as using objects in sessions and storing them in database tables.

  1. Using the Sleep and Wakeup Functions to Serialize Objects in PHP
  2. A quick look at how to define self-saving objects
  3. Using the “__sleep()” and “__wakeup()” magic functions
  4. Creating persisting objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
June 13, 2006

print this article



Indeed, it’s feasible to use the “__sleep()” and “__wakeup()” functions that you learned in the previous section to do something useful. In this particular case, I’ll utilize the prior “ObjectSaver” class as the parent for deriving a child class that will expose a property that persists, that is, keeps its value across different page requests.

First, here is the source code that corresponds to the “ObjectSaver” class:

class ObjectSaver{
    var $objectFile;
    function ObjectSaver($objectFile='defaultDir/objects.txt'){
    // save serialized object to file
    function save(){
            trigger_error('Error opening object
            trigger_error('Error writing data to object
    // fetch unserialized object from file
    function open(){
            trigger_error('Error fetching object from
        return $obj;
    // define '__sleep()' method
    function __sleep(){
        echo 'This method was called prior to serializing the
object!<br />';
        return array_keys(get_object_vars($this));       

    // define '__wakeup()' method
    function __wakeup(){
        echo 'This method was called after unserializing the
object!<br />';

Now that I've listed the above class, the next step consists of deriving a child class from it, to create a persisting object. That said, here is the signature of the “CounterSaver” class, which, as you’ll see in a few moments, is capable of maintaining the value of its “counter” property:

// 'CounterSaver' class extends 'ObjectSaver' class
// saves the value of 'counter' property to file and implements
persistence via
// the __sleep() and __wakeup() methods
class CounterSaver extends ObjectSaver{
    var $objectFile;
    var $counterFile;
    var $counter;
    function CounterSaver($objectFile='defaultDir/objects.txt'){
        // call constructor of parent class
    // overwrite '__sleep()' method
    function __sleep(){
            trigger_error('Error opening counter
            trigger_error('Error writing data to counter
        return array_keys(get_object_vars($this));
    // overwrite '__wakeup()' method
    function __wakeup(){
    // increment 'counter' property
    function incrementCounter(){
    // return counter property
    function getCounterValue(){
        return $this->counter;

As you can see, the child “CounterSaver” class listed above overrides the parent “__sleep()” and “__wakeup()” methods. It stores and retrieves the value of its $counter property, which is appropriately saved to the “counter.txt” file. In this example I used a raw text file to save the property, but this can be easily changed to utilize a database table or even a simple cookie.

Now, the question that comes up is the following: how does an object spawned from the “CounterSaver” class implement some kind of persistence? All right, that’s fairly easy to demonstrate. Just take a look at the following script, which illustrates the concept of a “persisting” object:

$countSaver=&new CounterSaver();
// open object (calls the __wakeup method)
// increment counter
// save object (calls the __sleep method)
echo $countSaver->getCounterValue();

When the above script is executed, an initial value of 1 is first assigned to the $counter property. However, if the page is reloaded several times, this value will be appropriately incremented, certainly a fact that demonstrates that the value of this property “persists” across different page requests. As you saw, a persistent object has been created by using the “__sleep()” and “__wakeup()” methods that were overridden by the “CounterSaver” class.

As you can see, the logic of the previous script is very simple: after instantiating a new “CounterSaver” object, the “open()” method is called, which also triggers internally the “__wakeup()” method. Then, the $counter property is incremented and finally saved to the text file by “save(),” which additionally calls behind the scenes the “__wakeup()” method. See how these two magic functions can be used for creating persisting objects within an application? I hope you do!

To wrap up

Over this second article, I explored the magic “__sleep()” and “__wakeup()” functions, which are called automatically by the PHP interpreter when the “serialize()/unserialize()” functions are used inside a class. By providing a specific implementation for these functions I showed you how to create objects that maintain the value of their properties across several page requests, without the need to work with sessions.

Of course, persistence of objects is a broader subject, but in this tutorial I demonstrated how to build simple persisting objects via the PHP built-in mechanism. Not bad at all, right?

In the final part of this series, I’ll be covering object serialization with sessions, as well as storing serialized objects in MySQL database tables. See you in the last part!  

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: