PHP
  Home arrow PHP arrow Page 2 - Using the Sleep and Wakeup Functions to Serialize Objects in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Using the Sleep and Wakeup Functions to Serialize Objects in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-06-13


    Table of Contents:
  • Using the Sleep and Wakeup Functions to Serialize Objects in PHP
  • A quick look at how to define self-saving objects
  • Using the “__sleep()” and “__wakeup()” magic functions
  • Creating persisting objects

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using the Sleep and Wakeup Functions to Serialize Objects in PHP - A quick look at how to define self-saving objects
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT