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

A quick look at how to define self-saving 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

A good place to start explaining some advanced concepts, such as the use of the “__sleep()” and “__wakeup()” PHP magic functions, and to introduce object persistence, is with precisely listing the definition for the example “ObjectSaver” class that you learned in the previous tutorial.

As you’ll certainly recall, this class was capable of saving and restoring itself via its own methods, which implies that any objects spawned from it can be considered self-saving objects.

Assuming that self-saving objects are pretty familiar to you, here is the corresponding signature of the “ObjectSaver” class, as I defined it originally in the first article. Please have a look:

class ObjectSaver{
    var $objectFile;
    function ObjectSaver($objectFile='defaultDir/objects.txt'){
        $this->objectFile=$objectFile;
        $this->save();
    }
    // save serialized object to file
    function save(){
        if(!$fp=fopen($this->objectFile,'w')){
            trigger_error('Error opening object
file',E_USER_ERROR);
        }
        if(!fwrite($fp,serialize($this))){
            trigger_error('Error writing data to object
file',E_USER_ERROR);
        }
        fclose($fp);
    }
    // fetch object from file
    function fetch(){
        if(!$obj=unserialize(file_get_contents($this-
>objectFile))){
            trigger_error('Error fetching object from
file',E_USER_ERROR);
        }
        return $obj;
    }
    // display message
    function displayMessage(){
        return 'This is an object that saves and retrieves itself
via its methods!';
    }
}

As you learned before, the above class initially exposed the “save()” and “fetch()” methods, which were handy for serializing and unserializing itself, and used a simple text file for storing its status. Naturally, this behavior can be modified easily, in order to use cookies or even a database table for saving different instances of the class.

Additionally, I added to the class the “displayMessage()” method, which can be used for showing a trivial message, in this way demonstrating how an object spawned from this class is capable of restoring itself. With reference to this capability, the below example clearly shows how to use an object which has been instanced from the “ObjectSaver” class:

$objSaver=&new ObjectSaver();
$newObj=$objSaver->fetch();
echo $newObj->displayMessage();

All right, after exemplifying the usage of the above class, I’m going to employ it to demonstrate how to use the “__sleep()” and “__wakeup()” PHP magic functions, in order to create objects that persist across different HTTP requests.

To learn how this will be achieved, please click on the link below and keep reading.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: