Zend_Serializer-Introduction.xml 3.7 KB

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