PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Review: the call magic function
    ( Page 2 of 4 )

    Before demonstrating how to implement and use the "__sleep()" and "__wakeup()" functions when serializing/unserializing objects, I'm going to take an extra step and reintroduce the example developed in the previous installment of this series. It was aimed at illustrating how to overload a single method of a sample class by using the handy "__call()" magic method.

    Having explained that, here is the entire source code of the aforementioned example class, which is very simple to follow:

    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];

    }

    }

    }

    As depicted above, the previous "User" class gives a concrete implementation to the "__call()" function, in order to catch all of the calls made to an undeclared "fetch()" method. As you can see, the purpose of this is simply to retrieve the values assigned to the properties of the class by using only this single method.

    Finally, to help you understand more clearly how the overloaded "__call()" function does its thing, here's a short script that shows this process in an approachable fashion. Take a look at it, please:

    // example on utilizing 'User' class with method overloading

    $user = new User();

    $user->fname = 'Alejandro';

    $user->lname = 'Gervasio';

    $user->email = 'alejandro@mydomain.com';

     

    // display user data

    echo 'First Name : ' . $user->fetch('fname') . ' Last Name : ' . $user->fetch('lname') . ' Email : ' . $user->fetch('email');

     

    /*

    display the following

    First Name : Alejandro Last Name : Gervasio Email : alejandro@mydomain.com

    */

    See how simple it is to overload methods in PHP 5? I bet you do! In this particular case, even when the "fetch()" method hasn't been declared within the "User" class, each time the script calls it, it'll be intercepted by the "__call()" function, in this way avoiding the triggering of a fatal error.

    So far, so good. At this stage, you hopefully recalled how to use the "__call()" magic function to recreate a simple case of method overloading. Therefore, it's time to explore other magic functions.

    So, as I explained at the beginning, in the section to come I'm going to discuss how to use the "__sleep()" and "__wakeup()" functions when serializing and unserializing objects.

    To learn more on this topic, now click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek