O'Reilly logo

PHP Advanced for the World Wide Web: Visual QuickPro Guide by Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required