6.13. Object Serialization

The serialize function, discussed in Chapter 15, converts variables, including objects, into strings. You can store the serialized variable in a file or send it across the network. Afterwards, unserialize can convert the string back into the appropriate value. As long as you define a class prior to unserializing an object of that class, PHP can successfully restore the object's properties and methods. In some situations you may need to prepare an object prior to serialization and likewise perform some procedure immediately after unserialization. For these purposes, PHP looks for the __sleep and __wakeup methods.

When serializing an object, PHP calls the __sleep method if it exists. After unserializing an object, PHP ...

Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.