Hudzilla.org - the homepage of Paul Hudson
Contents > Objects Wish List | Report Bug | About Me ]

6.13     Saving objects: __sleep(), __wakeup(), and get_object_vars()

This is NOT the latest copy of this book; click here for the latest version.

array get_object_vars ( object input)

Previously we covered how to save arrays in PHP using serialize(), unserialize(), urlencode(), and urldecode(). Saving objects works precisely the same way - you serialize() them into a string to make a format that can be saved, then urlencode() them to get a format that can be passed across the web without problem.

For example:

$poppy = new poodle('Poppy');
$safepoppy = urlencode(serialize($poppy));

There is one special feature with saving objects, though, and that is the fact that when serialize() and unserialize() are called, they will look for a __sleep() and __wakeup() function on the object they are working with respectively. These functions, which you have to provide yourself if you want them to do anything, allow you to properly keep an object working during its hibernation period (when it is just a string of data).

For example, when __sleep() is called, a logging object should save and close the file it was writing to, and when __wakeup() is called the object should reopen the file and carry on writing. Although __wakeup() need not return any value, __sleep() must return an array of the values you wish to have saved. If no __sleep() function is present, PHP will automatically save all variables, but you can mimic this behaviour in code by using the get_object_vars() function - more on that soon.

In code, our logger example would look like this:

class logger {
    private function
__sleep() {
        
$this->saveAndExit();
        
// return an empty array
        
return array();
    }

    private function
__wakeup() {
        
$this->openAndStart();
    }

    private function
saveAndExit() {
        
// ...[snip]...
    
}

Any objects of this class that are serialized would have __sleep() called on them, which would in turn call saveAndExit() - a mythical clean up function that saves the file and such. When objects of this class are unserialized they would have their __wakeup() function called, which would in turn call openAndStart().

To have PHP save all variables inside a __sleep() function, you need to use the get_object_vars() function. This takes an object as its only parameter, and returns an array of all the variables and their values in the object. You need to pass the variables to save back as the values in the array, so you should use the array_keys() function on the return value of get_object_vars(), like this:

private function __sleep() {
    
// do stuff here
    
return array_keys(get_object_vars($this));
}

If there is nothing in your __sleep() function other than returning the array, you should delete the function altogether as it is no different to what PHP will do by default.

If you find yourself needing to save objects, keep __sleep() and __wakeup() in mind - together they allow you to keep objects fully working across pages.





<< 6.12 Comparing objects with == and ===   6.14 Magic functions >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow