Home arrow PHP arrow Page 4 - The Sleep and Wakeup Magic Functions in PHP 5

The sleep and wakeup methods in action - PHP

Magic functions are an important part of the numerous improvements and additions that were introduced originally in PHP 5. They can be extremely handy when it comes to simplifying the execution of complex tasks. This is the fourth part of a seven-part series that showcases some of the more useful magic functions and how to implement them.

  1. The Sleep and Wakeup Magic Functions in PHP 5
  2. Review: the call magic function
  3. Introducing the sleep and wakeup functions
  4. The sleep and wakeup methods in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
June 16, 2009

print this article



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



displays the following


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: