File: unserialize.xml

package info (click to toggle)
php-doc 20061001-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 45,764 kB
  • ctags: 1,611
  • sloc: xml: 502,485; php: 7,645; cpp: 500; makefile: 297; perl: 161; sh: 141; awk: 28
file content (137 lines) | stat: -rw-r--r-- 4,821 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.19 $ -->
<!-- splitted from ./en/functions/var.xml, last change in rev 1.2 -->
  <refentry id="function.unserialize">
   <refnamediv>
    <refname>unserialize</refname>
    <refpurpose>
     Creates a PHP value from a stored representation
    </refpurpose>
   </refnamediv>
   <refsect1>
    <title>Description</title>
     <methodsynopsis>
      <type>mixed</type><methodname>unserialize</methodname>
      <methodparam><type>string</type><parameter>str</parameter></methodparam>
     </methodsynopsis>
    <simpara>
     <function>unserialize</function> takes a single serialized
     variable (see <function>serialize</function>) and converts it
     back into a PHP value.  The converted value is returned, and can
     be a <type>boolean</type>, <type>integer</type>, <type>float</type>,
     <type>string</type>, <type>array</type> or <type>object</type>.
     In case the passed string is not unserializeable, &false; is returned and
     <literal>E_NOTICE</literal> is issued.
    </simpara>
    <warning>
     <para>
      &false; is returned both in the case of an error and if unserializing
      the serialized &false; value. It is possible to catch this special case by
      comparing <parameter>str</parameter> with
      <literal>serialize(false)</literal> or by catching the issued
      <literal>E_NOTICE</literal>.
     </para>
    </warning>
    <note>
     <title>unserialize_callback_func directive</title>
     <para>
      It's possible to set a callback-function which will be called,
      if an undefined class should be instantiated during unserializing.
      (to prevent getting an incomplete <type>object</type> "__PHP_Incomplete_Class".)
      Use your &php.ini;, <function>ini_set</function> or &htaccess; 
      to define 'unserialize_callback_func'.  Everytime an undefined class
      should be instantiated, it'll be called.  To disable this feature just
      empty this setting.  Also note that the directive
      unserialize_callback_func directive became 
      available in PHP 4.2.0. 
     </para>
    </note>
    <para>
     If the variable being unserialized is an object, after successfully 
     reconstructing the object PHP will automatically attempt to call the
     <function>__wakeup</function> member function (if it exists). 
     <example>
      <title>unserialize_callback_func example</title>
      <programlisting role="php">
<![CDATA[
<?php
$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

// unserialize_callback_func directive available as of PHP 4.2.0
ini_set('unserialize_callback_func', 'mycallback'); // set your callback_function

function mycallback($classname) 
{
    // just include a file containing your classdefinition
    // you get $classname to figure out which classdefinition is required
}
?>
]]>
      </programlisting>
     </example>
    </para>
    <note>
     <para>
      In PHP 3, methods are not preserved when unserializing a
      serialized object. That limitation was removed in PHP 4 as both 
      properties and methods are now restored.  Please see the <link
      linkend="language.oop.serialization">Serializing Objects</link>
      section of <link linkend="language.oop">Classes and
      Objects</link> or more information.
     </para>
    </note>
    <para>
     <example>
      <title><function>unserialize</function> example</title>
      <programlisting role="php">
<![CDATA[
<?php
// Here, we use unserialize() to load session data to the
// $session_data array from the string selected from a database.
// This example complements the one described with serialize().

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn, "SELECT data FROM sessions WHERE id = ?");
$sqldata = array ($PHP_AUTH_USER);
if (!odbc_execute($stmt, &$sqldata) || !odbc_fetch_into($stmt, &$tmp)) {
    // if the execute or fetch fails, initialize to empty array
    $session_data = array();
} else {
    // we should now have the serialized data in $tmp[0].
    $session_data = unserialize($tmp[0]);
    if (!is_array($session_data)) {
        // something went wrong, initialize to empty array
        $session_data = array();
    }
}
?>
]]>
      </programlisting>
     </example>
    </para>
   <para>
    See also <function>serialize</function>.
   </para>
   </refsect1>
  </refentry>

<!-- 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:"../../../../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
-->