File: serialization.xml

package info (click to toggle)
php-doc 20241205~git.dfcbb86%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 70,956 kB
  • sloc: xml: 968,269; php: 23,883; javascript: 671; sh: 177; makefile: 37
file content (113 lines) | stat: -rw-r--r-- 3,723 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- TODO Rewrite to remove usage of "you" and talk about __serialize/_unserialize -->
 <sect1 xml:id="language.oop5.serialization" xmlns="http://docbook.org/ns/docbook">
  <title>Serializing objects - objects in sessions</title>
  <titleabbrev>Object Serialization</titleabbrev>

  <para>
   <function>serialize</function> returns a string containing a
   byte-stream representation of any value that can be stored in
   PHP. <function>unserialize</function> can use this string to
   recreate the original variable values. Using serialize to
   save an object will save all variables in an object.  The
   methods in an object will not be saved, only the name of
   the class.
  </para>
  
  <para>
   In order to be able to <function>unserialize</function> an object, the
   class of that object needs to be defined. That is, if you have an object
    of class A and serialize this, you'll
   get a string that refers to class A and contains all values of variables
   contained in it. If you want to be able to unserialize
   this in another file, an object of class A, the
   definition of class A must be present in that file first.
   This can be done for example by storing the class definition of class A
   in an include file and including this file or making use of the
   <function>spl_autoload_register</function> function.
  </para>
  
  <informalexample>
   <programlisting role="php">
<![CDATA[
<?php
// A.php:
  
  class A {
      public $one = 1;
    
      public function show_one() {
          echo $this->one;
      }
  }
  
// page1.php:

  include "A.php";
  
  $a = new A;
  $s = serialize($a);
  // store $s somewhere where page2.php can find it.
  file_put_contents('store', $s);

// page2.php:
  
  // this is needed for the unserialize to work properly.
  include "A.php";

  $s = file_get_contents('store');
  $a = unserialize($s);

  // now use the function show_one() of the $a object.  
  $a->show_one();
?>
]]>
   </programlisting>
  </informalexample>

  <para>
   It is strongly recommended that if an application serializes objects, for use
   later in the application, that the application includes the class definition
   for that object throughout the application. Not doing so might result in an
   object being unserialized without a class definition, which will result in
   PHP giving the object a class of <classname>__PHP_Incomplete_Class_Name</classname>,
   which has no methods and would render the object useless.
  </para>
  
  <para>
   So if in the example above <varname>$a</varname> became part of a session
   by adding a new key to the <varname>$_SESSION</varname> superglobal array, you should include the
   file <literal>A.php</literal> on all of your pages, not only <filename>page1.php</filename>
   and <filename>page2.php</filename>.
  </para>

  <para>
   Beyond the above advice, note that you can also hook into the serialization
   and unserialization events on an object using the
   <link linkend="object.sleep">__sleep()</link> and
   <link linkend="object.wakeup">__wakeup()</link> methods. Using
   <link linkend="object.sleep">__sleep()</link> also allows you to only
   serialize a subset of the object's properties.
  </para>
 </sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->