Objects that you create in PHP are stored as binary data in memory.
PHP provides two functions to help you with this:
Method | Description |
---|---|
serialize() | converts an object - properties, methods, and all - into a string of text |
unserialize() | takes a string created by serialize() and turns it back into a usable object |
The following example shows these two functions in action:
<?php class Person {/*from ww w . j ava 2 s.c om*/ public $age; } $tom = new Person(); $tom->age = 28; $tomString = serialize($tom); echo "::::'$tomString'\n"; echo "Converting '$tomString' back to an object... \n"; $obj = unserialize($tomString); echo "tom's age is: $obj->age \n"; ?>
This code creates a simple Person class with one property, $age.
It then creates a new Person object, $tom, and sets its $age property to 28.
It calls serialize() to convert the object to a string, which it displays.
Finally, it converts the string back into a new object,$obj, then displays its $obj->age property (28).
You can actually use serialize() and unserialize() on any PHP value, not just objects.
When you serialize an object, PHP attempts to call a method with the name __sleep() inside the object.
You can use this method to do anything that's required before the object is serialized.
You can create a __wakeup() method that is called when the object is unserialized.
__sleep() is useful for cleaning up an object prior to serializing it, in the same way that you might clean up in a destructor method.
PHP expects your __sleep() method to return an array of names of properties to preserve in the serialized string.
You can use this to limit the number of properties stored in the string.
<?php class User { public $username; public $password; public $loginsToday; public function __sleep() { // (Clean up; close database handles, etc) return array("username","password"); }/*from w w w. j av a 2s . co m*/ } $user = new User; $user->username ="tom"; $user->password ="mypassword"; $user->loginsToday = 3; echo "The original user object: \n"; print_r($user); echo "Serializing the object... \n"; $userString = serialize($user); echo "The user is now serialized in the following string: \n"; echo "$userString \n"; echo "Converting the string back to an object... \n"; $obj = unserialize($userString); echo "The unserialized object: \n"; print_r($obj); echo "\n"; ?>
Here, we don't care about preserving the number of times the user has logged in today, so the __sleep() method only returns the "username" and "password" property names.
When the object is restored from the string, the $loginsToday property is empty.
To preserve all your object's properties, use the built-in get_object_vars() function to get an associative array of all the properties in the object.
Then use the array_keys() function to get just the property names as an array, which you can then return from your __sleep() method:
<?php class User { public $username; public $password; public $loginsToday; public function __sleep() { return array_keys(get_object_vars($this)); }/*from w w w . j av a 2s . co m*/ public function __wakeup() { echo "wakeup \n"; } } $user = new User; $userString = serialize($user); $obj = unserialize($userString); ?>