1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.serializer.introduction">
4 <title>Introduction</title>
7 <classname>Zend_Serializer</classname> provides an adapter based interface to simply
8 generate storable representation of <acronym>PHP</acronym> types by different facilities,
12 <example id="zend.serializer.introduction.example.dynamic">
13 <title>Using Zend_Serializer dynamic interface</title>
16 To instantiate a serializer you should use the factory method with the name of the
20 <programlisting language="php"><![CDATA[
21 $serializer = Zend_Serializer::factory('PhpSerialize');
22 // Now $serializer is an instance of Zend_Serializer_Adapter_AdapterInterface,
23 // specifically Zend_Serializer_Adapter_PhpSerialize
26 $serialized = $serializer->serialize($data);
27 // now $serialized is a string
29 $unserialized = $serializer->unserialize($serialized);
30 // now $data == $unserialized
31 } catch (Zend_Serializer_Exception $e) {
38 The method <methodname>serialize()</methodname> generates a storable string. To regenerate
39 this serialized data you can simply call the method <methodname>unserialize()</methodname>.
43 Any time an error is encountered serializing or unserializing,
44 <classname>Zend_Serializer</classname> will throw a
45 <classname>Zend_Serializer_Exception</classname>.
49 To configure a given serializer adapter, you can optionally add an array or an instance of
50 <classname>Zend_Config</classname> to the <methodname>factory()</methodname> or to the
51 <methodname>serialize()</methodname> and <methodname>unserialize()</methodname> methods:
54 <programlisting language="php"><![CDATA[
55 $serializer = Zend_Serializer::factory('Wddx', array(
56 'comment' => 'serialized by Zend_Serializer',
60 $serialized = $serializer->serialize(
62 array('comment' => 'change comment')
65 $unserialized = $serializer->unserialize(
67 array(/* options for unserialize */)
69 } catch (Zend_Serializer_Exception $e) {
75 Options passed to the <methodname>factory()</methodname> are valid for the instantiated
76 object. You can change these options using the <methodname>setOption(s)</methodname> method.
77 To change one or more options only for a single call, pass them as the second argument to
78 either the <methodname>serialize()</methodname> or <methodname>unserialize()</methodname>
82 <example id="zend.serializer.introduction.example.static.php">
83 <title>Using the Zend_Serializer static interface</title>
86 You can register a specific serializer adapter as a default serialization adapter for
87 use with <classname>Zend_Serializer</classname>. By default, the
88 <classname>PhpSerialize</classname> adapter will be registered, but you can change this
89 option using the <methodname>setDefaultAdapter()</methodname> static method.
92 <programlisting language="php"><![CDATA[
93 Zend_Serializer::setDefaultAdapter('PhpSerialize', $options);
95 $serializer = Zend_Serializer::factory('PhpSerialize', $options);
96 Zend_Serializer::setDefaultAdapter($serializer);
99 $serialized = Zend_Serializer::serialize($data, $options);
100 $unserialized = Zend_Serializer::unserialize($serialized, $options);
101 } catch (Zend_Serializer_Exception $e) {