PHP
  Home arrow PHP arrow Page 4 - 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 - The sleep and wakeup methods in action
    ( Page 4 of 4 )

    The best way to understand how the previously implemented "__sleep()" and "__wakeup()" magic methods do their business is by means of a concrete example that uses the "serialize()" and "unserialize()" PHP functions with an instance of the previous "User" class.

    Based on this idea, below I created a simple script that performs this serialization/unserialization process in an approachable fashion. Look at it, please:

    // example on using the 'User' class with the '__sleep()' and '__wakeup()' methods

     

    $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');

    /*

    displays the following

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

    */

     

    // serialize user object

    $user = serialize($user);

    /*

    displays the following

    Serializing user object

    */

     

    // unserialize user object

    var_dump(unserialize($user));

    /*

    displays the following

    object(User)[1]

    public 'fname' => string 'Alejandro' (length=9)

    */

    As you can see in the above script, each time an instance of the previous "User" class is serialized and unserialized, the PHP engine calls internally the "__sleep()" and "__wakeup()" methods, which display a simple message on the browser. Of course, it's possible to give a more useful and complex implementation of these methods, but hopefully the code sample shown above will give you a more clear idea of how to do this all by yourself.

    Feel free to tweak all of the examples shown in this tutorial, so you can arm yourself with a better grounding in using these magic functions in PHP 5.

    Final thoughts

    That's it for now. In this fourth chapter of the series I provided you with a brief introduction to using the "__sleep()" and "__wakeup()" magic functions when serializing and unserializing objects.

    Logically, as I said before it's possible to implement these functions in a more useful way, for instance for creating persistent objects that save themselves to a file or a database when they're serialized. Indeed, the possibilities are pretty numerous.

    In the next article, I'm going to continue this exploration of magic functions that come with PHP 5. That particular tutorial will be focused on discussing the "__clone()" function.

    Don't miss the upcoming part!



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek