Serializing Objects

Because of the unique nature of objects, they cannot be as easily managed as strings or numbers. This makes them more difficult to store in a database, pass along to a second PHP script, or set as a cookie. To handle such issues, PHP has the serialize() function, which takes a variable and turns it into a more manageable version of itself. It works like so:

$variable = serialize ($object); 

To return the serialized variable back into its standard form, use the unserialize() function.

$object = unserialize ($variable); 

The only caveat to using these two functions with objects is that the page that unserializes the variable must have access to the class definitions.

As a crude analogy, say you had created a really cool toy ...

Get PHP Advanced for the World Wide Web: Visual QuickPro Guide 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.