PHP
  Home arrow PHP arrow Page 3 - The Sleep and Wakeup Magic Functions in PHP 5
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

The Sleep and Wakeup Magic Functions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-06-16


    Table of Contents:
  • The Sleep and Wakeup Magic Functions in PHP 5
  • Review: the call magic function
  • Introducing the sleep and wakeup functions
  • The sleep and wakeup methods in action

  • 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


    The Sleep and Wakeup Magic Functions in PHP 5 - Introducing the sleep and wakeup functions
    ( Page 3 of 4 )

    As I explained in the introduction, PHP 5 also includes a pair of complementary magic functions called "__sleep()" and "__wakeup()," which are transparently invoked when an object is being serialized and unserialized respectively.

    In theory, these functions should be very easy to grasp, but it'd be even better to back up these concepts with some illustrative examples. So, below I redefined the "User" class that you saw before by giving a basic implementation to the "__sleep()" and "__wakeup()" methods.

    Take a look at the class's source code, which now looks like this:

    class User

    {

    // constructor (not implemented)

    public function _construct(){}

     

    // set undeclared property in a restrictive way

    public function __set($property, $value)

    {

    if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

    {

    $this->$property = $value;

    }

    }

     

    // get declared property

    public function __get($property)

    {

    if (isset($this->$property))

    {

    return $this->$property;

    }

    }

     

    // single point to fetch user data

    public function __call($method, $args)

    {

    if ($method === 'fetch' AND empty($args) === FALSE)

    {

    return $this->$args[0];

    }

    }

     

    // implement __sleep() method

    public function __sleep()

    {

    echo 'Serializing user object';

    return array('fname');

    }

     

    // implement __wakeup() method

    public function __wakeup()

    {

    echo 'Unserializing user object';

    }

    }

    Now, apart from using the already familiar "__call()" function, the above "User" class also gives a concrete implementation of the "__sleep()" and "__wakeup()" methods. It's admittedly very trivial, since they'll only display a message on the browser when being called by the PHP engine. Despite the simplicity of this particular example, it should be useful enough to demonstrate how to utilize the "__sleep()" and "__wakeup()" functions within a class.

    However, the "User" class itself won't do anything useful until an instance of it is serialized or unserialized, since the mentioned magic methods will be called upon the occurrence of these specific events.

    So, it's necessary to create a script that performs these tasks in a simple manner. Thus, in the last section of this tutorial I'm going to develop a wrapping example that will show what happens when an instance of the previous "User" class in serialized and unserialized sequentially.

    To learn how this final example will be created, click on the link that appears below and read the following section.



     
     
    >>> 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 1 Hosted by Hostway
    Stay green...Green IT