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

Introducing the sleep and wakeup functions - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: