Zend_Serializer-Introduction.xml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.serializer.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_Serializer</classname> provides an adapter based interface to simply
  7. generate storable representation of <acronym>PHP</acronym> types by different facilities,
  8. and recover.
  9. </para>
  10. <example id="zend.serializer.introduction.example.dynamic">
  11. <title>Using Zend_Serializer dynamic interface</title>
  12. <para>
  13. To instantiate a serializer you should use the factory method with the name of the
  14. adapter:
  15. </para>
  16. <programlisting language="php"><![CDATA[
  17. $serializer = Zend_Serializer::factory('PhpSerialize');
  18. // Now $serializer is an instance of Zend_Serializer_Adapter_AdapterInterface,
  19. // specifically Zend_Serializer_Adapter_PhpSerialize
  20. try {
  21. $serialized = $serializer->serialize($data);
  22. // now $serialized is a string
  23. $unserialized = $serializer->unserialize($serialized);
  24. // now $data == $unserialized
  25. } catch (Zend_Serializer_Exception $e) {
  26. echo $e;
  27. }
  28. ]]></programlisting>
  29. </example>
  30. <para>
  31. The method <methodname>serialize()</methodname> generates a storable string. To regenerate
  32. this serialized data you can simply call the method <methodname>unserialize()</methodname>.
  33. </para>
  34. <para>
  35. Any time an error is encountered serializing or unserializing,
  36. <classname>Zend_Serializer</classname> will throw a
  37. <classname>Zend_Serializer_Exception</classname>.
  38. </para>
  39. <para>
  40. To configure a given serializer adapter, you can optionally add an array or an instance of
  41. <classname>Zend_Config</classname> to the <methodname>factory()</methodname> or to the
  42. <methodname>serialize()</methodname> and <methodname>unserialize()</methodname> methods:
  43. </para>
  44. <programlisting language="php"><![CDATA[
  45. $serializer = Zend_Serializer::factory('Wddx', array(
  46. 'comment' => 'serialized by Zend_Serializer',
  47. ));
  48. try {
  49. $serialized = $serializer->serialize(
  50. $data,
  51. array('comment' => 'change comment')
  52. );
  53. $unserialized = $serializer->unserialize(
  54. $serialized,
  55. array(/* options for unserialize */)
  56. );
  57. } catch (Zend_Serializer_Exception $e) {
  58. echo $e;
  59. }
  60. ]]></programlisting>
  61. <para>
  62. Options passed to the <methodname>factory()</methodname> are valid for the instantiated
  63. object. You can change these options using the <methodname>setOption(s)</methodname> method.
  64. To change one or more options only for a single call, pass them as the second argument to
  65. either the <methodname>serialize()</methodname> or <methodname>unserialize()</methodname>
  66. method.
  67. </para>
  68. <example id="zend.serializer.introduction.example.static.php">
  69. <title>Using the Zend_Serializer static interface</title>
  70. <para>
  71. You can register a specific serializer adapter as a default serialization adapter for
  72. use with <classname>Zend_Serializer</classname>. By default, the
  73. <classname>PhpSerialize</classname> adapter will be registered, but you can change this
  74. option using the <methodname>setDefaultAdapter()</methodname> static method.
  75. </para>
  76. <programlisting language="php"><![CDATA[
  77. Zend_Serializer::setDefaultAdapter('PhpSerialize', $options);
  78. // or
  79. $serializer = Zend_Serializer::factory('PhpSerialize', $options);
  80. Zend_Serializer::setDefaultAdapter($serializer);
  81. try {
  82. $serialized = Zend_Serializer::serialize($data, $options);
  83. $unserialized = Zend_Serializer::unserialize($serialized, $options);
  84. } catch (Zend_Serializer_Exception $e) {
  85. echo $e;
  86. }
  87. ]]></programlisting>
  88. </example>
  89. </sect1>
  90. <!--
  91. vim:se ts=4 sw=4 et:
  92. -->