Zend_Serializer-Introduction.xml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <classname>Zend_Serializer</classname> 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>un-/serialize</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($data, array('comment' => 'change comment'));
  49. $unserialized = $serializer->unserialize($serialized, array(/* options for unserialize */));
  50. } catch (Zend_Serializer_Exception $e) {
  51. echo $e;
  52. }
  53. ]]></programlisting>
  54. <para>
  55. Options passed to the <methodname>factory</methodname> are valid for the instantiated
  56. object. You can change these options using the <methodname>setOption(s)</methodname> method.
  57. To change one or more options only for a single call, pass them as the second argument to
  58. either the <methodname>serialize</methodname> or <methodname>unserialize</methodname>
  59. method.
  60. </para>
  61. <sect2 id="zend.serializer.introduction.example.static.php">
  62. <title>Using the Zend_Serializer static interface</title>
  63. <para>
  64. You can register a specific serializer adapter as a default serialization adapter for
  65. use with <classname>Zend_Serializer</classname>. By default, the
  66. <classname>PhpSerialize</classname> adapter will be registered, but you can change this
  67. option using the <methodname>setDefaultAdapter()</methodname> static method.
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. Zend_Serializer::setDefaultAdapter('PhpSerialize', $options);
  71. // or
  72. $serializer = Zend_Serializer::factory('PhpSerialize', $options);
  73. Zend_Serializer::setDefaultAdapter($serializer);
  74. try {
  75. $serialized = Zend_Serializer::serialize($data, $options);
  76. $unserialized = Zend_Serializer::unserialize($serialized, $options);
  77. } catch (Zend_Serializer_Exception $e) {
  78. echo $e;
  79. }
  80. ]]></programlisting>
  81. </sect2>
  82. </sect1>
  83. <!--
  84. vim:se ts=4 sw=4 et:
  85. -->