Introduction
Zend_Serializer provides an adapter based interface to simply
generate storable representation of php types by different facilities, and recover.
Using Zend_Serializer dynamic interface
To instantiate a serializer you should use the factory method with the name of the
adapter:
serialize($data);
// now $serialized is a string
$unserialized = $serializer->unserialize($serialized);
// now $data == $unserialized
} catch (Zend_Serializer_Exception $e) {
echo $e;
}
]]>
The method serialize generates a storable string. To regenerate
this serialized data you can simply call the method unserialize.
Any time an error is encountered serializing or unserializing,
Zend_Serializer will throw a
Zend_Serializer_Exception.
To configure a given serializer adapter, you can optionally add an array or an instance of
Zend_Config to the factory or to the
un-/serialize methods:
'serialized by Zend_Serializer',
));
try {
$serialized = $serializer->serialize($data, array('comment' => 'change comment'));
$unserialized = $serializer->unserialize($serialized, array(/* options for unserialize */));
} catch (Zend_Serializer_Exception $e) {
echo $e;
}
]]>
Options passed to the factory are valid for the instantiated
object. You can change these options using the setOption(s) method.
To change one or more options only for a single call, pass them as the second argument to
either the serialize or unserialize
method.
Using the Zend_Serializer static interface
You can register a specific serializer adapter as a default serialization adapter for
use with Zend_Serializer. By default, the
PhpSerialize adapter will be registered, but you can change this
option using the setDefaultAdapter() static method.