2
0

Zend_Serializer-Introduction.xml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 20225 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.serializer.introduction">
  5. <title>Introduction</title>
  6. <para>
  7. <classname>Zend_Serializer</classname> provides an adapter based interface to simply
  8. generate storable representation of php types by different facilities, and recover.
  9. </para>
  10. <example id="zend.serializer.introduction.example.dynamic">
  11. <title>Using <classname>Zend_Serializer</classname> 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>un-/serialize</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($data, array('comment' => 'change comment'));
  50. $unserialized = $serializer->unserialize($serialized, array(/* options for unserialize */));
  51. } catch (Zend_Serializer_Exception $e) {
  52. echo $e;
  53. }
  54. ]]></programlisting>
  55. <para>
  56. Options passed to the <methodname>factory</methodname> are valid for the instantiated
  57. object. You can change these options using the <methodname>setOption(s)</methodname> method.
  58. To change one or more options only for a single call, pass them as the second argument to
  59. either the <methodname>serialize</methodname> or <methodname>unserialize</methodname>
  60. method.
  61. </para>
  62. <sect2 id="zend.serializer.introduction.example.static.php">
  63. <title>Using the Zend_Serializer static interface</title>
  64. <para>
  65. You can register a specific serializer adapter as a default serialization adapter for
  66. use with <classname>Zend_Serializer</classname>. By default, the
  67. <classname>PhpSerialize</classname> adapter will be registered, but you can change this
  68. option using the <methodname>setDefaultAdapter()</methodname> static method.
  69. </para>
  70. <programlisting language="php"><![CDATA[
  71. Zend_Serializer::setDefaultAdapter('PhpSerialize', $options);
  72. // or
  73. $serializer = Zend_Serializer::factory('PhpSerialize', $options);
  74. Zend_Serializer::setDefaultAdapter($serializer);
  75. try {
  76. $serialized = Zend_Serializer::serialize($data, $options);
  77. $unserialized = Zend_Serializer::unserialize($serialized, $options);
  78. } catch (Zend_Serializer_Exception $e) {
  79. echo $e;
  80. }
  81. ]]></programlisting>
  82. </sect2>
  83. </sect1>
  84. <!--
  85. vim:se ts=4 sw=4 et:
  86. -->