Saving objects

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() {
        // return an empty array
        return array();

    private function __wakeup() {

    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.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Magic functions >>

Previous chapter: Comparing objects with == and ===

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.