reference/var/functions/serialize.xml
8cdc6621f9826d04abc3e50438c010804d7e8683
...
...
@@ -13,7 +13,7 @@
13
13
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
14
14
</methodsynopsis>
15
15
<para>
16
-
Generates a storable representation of a value
16
+
Generates a storable representation of a value.
17
17
</para>
18
18
<para>
19
19
This is useful for storing or passing PHP values around without
...
...
@@ -34,19 +34,20 @@
34
34
<listitem>
35
35
<para>
36
36
The value to be serialized. <function>serialize</function>
37
-
handles all types, except the <type>resource</type>-type.
37
+
handles all types, except the <type>resource</type>-type and some <type>object</type>s (see note below).
38
38
You can even <function>serialize</function> arrays that contain
39
39
references to itself. Circular references inside the array/object you
40
40
are serializing will also be stored. Any other
41
41
reference will be lost.
42
42
</para>
43
43
<para>
44
-
When serializing objects, PHP will attempt to call the member function
45
-
<link linkend="language.oop5.magic">__sleep</link> prior to serialization.
44
+
When serializing objects, PHP will attempt to call the member functions
45
+
<link linkend="object.serialize">__serialize()</link> or
46
+
<link linkend="object.sleep">__sleep()</link> prior to serialization.
46
47
This is to allow the object to do any last minute clean-up, etc. prior
47
48
to being serialized. Likewise, when the object is restored using
48
-
<function>unserialize</function> the <link
49
-
linkend="language.oop5.magic">__wakeup</link> member function is called.
49
+
<function>unserialize</function> the <link linkend="object.unserialize">__unserialize()</link> or
50
+
<link linkend="object.wakeup">__wakeup()</link> member function is called.
50
51
</para>
51
52
<note>
52
53
<para>
...
...
@@ -67,6 +68,12 @@
67
68
Returns a string containing a byte-stream representation of
68
69
<parameter>value</parameter> that can be stored anywhere.
69
70
</para>
71
+
<para>
72
+
Note that this is a binary string which may include null bytes, and needs
73
+
to be stored and handled as such. For example,
74
+
<function>serialize</function> output should generally be stored in a BLOB
75
+
field in a database, rather than a CHAR or TEXT field.
76
+
</para>
70
77
</refsect1>
71
78

72
79
<refsect1 role="examples">
...
...
@@ -88,7 +95,7 @@ $sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
88
95
if (!odbc_execute($stmt, $sqldata)) {
89
96
$stmt = odbc_prepare($conn,
90
97
"INSERT INTO sessions (id, data) VALUES(?, ?)");
91
-
if (!odbc_execute($stmt, $sqldata)) {
98
+
if (!odbc_execute($stmt, array_reverse($sqldata))) {
92
99
/* Something went wrong.. */
93
100
}
94
101
}
...
...
@@ -99,37 +106,27 @@ if (!odbc_execute($stmt, $sqldata)) {
99
106
</para>
100
107
</refsect1>
101
108

102
-
<refsect1 role="changelog">
103
-
&reftitle.changelog;
104
-
<para>
105
-
<informaltable>
106
-
<tgroup cols="2">
107
-
<thead>
108
-
<row>
109
-
<entry>&Version;</entry>
110
-
<entry>&Description;</entry>
111
-
</row>
112
-
</thead>
113
-
<tbody>
114
-
<row>
115
-
<entry>4.0.7</entry>
116
-
<entry>
117
-
The object serialization process was fixed.
118
-
</entry>
119
-
</row>
120
-
</tbody>
121
-
</tgroup>
122
-
</informaltable>
123
-
</para>
124
-
</refsect1>
125
-

126
109
<refsect1 role="notes">
127
110
&reftitle.notes;
128
111
<note>
129
112
<para>
130
-
It is not possible to serialize PHP built-in objects.
113
+
Note that many built-in PHP objects cannot be serialized. However, those with
114
+
this ability either implement the <interfacename>Serializable</interfacename> interface or the
115
+
magic <link linkend="object.serialize">__serialize()</link>/<link linkend="object.unserialize">__unserialize()</link>
116
+
or <link linkend="object.sleep">__sleep()</link>/<link linkend="object.wakeup">__wakeup()</link> methods. If an
117
+
internal class does not fulfill any of those requirements, it cannot reliably be
118
+
serialized.
119
+
</para>
120
+
<para>
121
+
There are some historical exceptions to the above rule, where some internal objects
122
+
could be serialized without implementing the interface or exposing the methods.
131
123
</para>
132
124
</note>
125
+
<warning>
126
+
<para>
127
+
When <function>serialize</function> serializes objects, the leading backslash is not included in the class name of namespaced classes for maximum compatibility.
128
+
</para>
129
+
</warning>
133
130
</refsect1>
134
131

135
132
<refsect1 role="seealso">
...
...
@@ -137,7 +134,13 @@ if (!odbc_execute($stmt, $sqldata)) {
137
134
<para>
138
135
<simplelist>
139
136
<member><function>unserialize</function></member>
137
+
<member><function>var_export</function></member>
138
+
<member><function>json_encode</function></member>
140
139
<member><link linkend="language.oop5.serialization">Serializing Objects</link></member>
140
+
<member><link linkend="object.sleep">__sleep()</link></member>
141
+
<member><link linkend="object.wakeup">__wakeup()</link></member>
142
+
<member><link linkend="object.serialize">__serialize()</link></member>
143
+
<member><link linkend="object.unserialize">__unserialize()</link></member>
141
144
</simplelist>
142
145
</para>
143
146
</refsect1>
144
147