<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >unserialize</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="Variáveis, Funções" HREF="ref.variables.html"><LINK REL="PREVIOUS" TITLE="strval" HREF="function.strval.html"><LINK REL="NEXT" TITLE="unset" HREF="function.unset.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.strval.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.unset.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.unserialize" ></A >unserialize</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN230485" ></A ><P > (PHP 3 >= 3.0.5, PHP 4, PHP 5)</P >unserialize -- Creates a PHP value from a stored representation </DIV ><DIV CLASS="refsect1" ><A NAME="AEN230488" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >unserialize</B > ( string str )<BR ></BR ><P > <B CLASS="function" >unserialize()</B > takes a single serialized variable (see <A HREF="function.serialize.html" ><B CLASS="function" >serialize()</B ></A >) and converts it back into a PHP value. The converted value is returned, and can be a <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A >, <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >, <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A >, <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A >, <A HREF="language.types.array.html" ><B CLASS="type" >array</B ></A > or <A HREF="language.types.object.html" ><B CLASS="type" >object</B ></A >. In case the passed string is not unserializeable, <TT CLASS="constant" ><B >FALSE</B ></TT > is returned and <TT CLASS="literal" >E_NOTICE</TT > is issued. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenção</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > <TT CLASS="constant" ><B >FALSE</B ></TT > is returned both in the case of an error and if unserializing the serialized <TT CLASS="constant" ><B >FALSE</B ></TT > value. It is possible to catch this special case by comparing <CODE CLASS="parameter" >str</CODE > with <TT CLASS="literal" >serialize(false)</TT > or by catching the issued <TT CLASS="literal" >E_NOTICE</TT >. </P ></TD ></TR ></TABLE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >unserialize_callback_func directive: </B > 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 <A HREF="language.types.object.html" ><B CLASS="type" >object</B ></A > "__PHP_Incomplete_Class".) Use your <TT CLASS="filename" >php.ini</TT >, <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A > or <TT CLASS="filename" >.htaccess</TT > 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. </P ></BLOCKQUOTE ></DIV ><P > If the variable being unserialized is an object, after successfully reconstructing the object PHP will automatically attempt to call the <B CLASS="function" >__wakeup()</B > member function (if it exists). <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN230523" ></A ><P ><B >Exemplo 1. unserialize_callback_func example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$serialized_object</font><font color="#007700">=</font><font color="#DD0000">'O:1:"a":1:{s:5:"value";s:3:"100";}'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// unserialize_callback_func directive available as of PHP 4.2.0<br /></font><font color="#0000BB">ini_set</font><font color="#007700">(</font><font color="#DD0000">'unserialize_callback_func'</font><font color="#007700">, </font><font color="#DD0000">'mycallback'</font><font color="#007700">); </font><font color="#FF8000">// set your callback_function<br /><br /></font><font color="#007700">function </font><font color="#0000BB">mycallback</font><font color="#007700">(</font><font color="#0000BB">$classname</font><font color="#007700">) <br />{<br /> </font><font color="#FF8000">// just include a file containing your classdefinition<br /> // you get $classname to figure out which classdefinition is required<br /></font><font color="#007700">}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B > 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 <A HREF="language.oop.serialization.html" >Serializing Objects</A > section of <A HREF="language.oop.html" >Classes and Objects</A > or more information. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN230531" ></A ><P ><B >Exemplo 2. <B CLASS="function" >unserialize()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Here, we use unserialize() to load session data to the<br />// $session_data array from the string selected from a database.<br />// This example complements the one described with serialize().<br /><br /></font><font color="#0000BB">$conn </font><font color="#007700">= </font><font color="#0000BB">odbc_connect</font><font color="#007700">(</font><font color="#DD0000">"webdb"</font><font color="#007700">, </font><font color="#DD0000">"php"</font><font color="#007700">, </font><font color="#DD0000">"chicken"</font><font color="#007700">);<br /></font><font color="#0000BB">$stmt </font><font color="#007700">= </font><font color="#0000BB">odbc_prepare</font><font color="#007700">(</font><font color="#0000BB">$conn</font><font color="#007700">, </font><font color="#DD0000">"SELECT data FROM sessions WHERE id = ?"</font><font color="#007700">);<br /></font><font color="#0000BB">$sqldata </font><font color="#007700">= array (</font><font color="#0000BB">$PHP_AUTH_USER</font><font color="#007700">);<br />if (!</font><font color="#0000BB">odbc_execute</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">, &</font><font color="#0000BB">$sqldata</font><font color="#007700">) || !</font><font color="#0000BB">odbc_fetch_into</font><font color="#007700">(</font><font color="#0000BB">$stmt</font><font color="#007700">, &</font><font color="#0000BB">$tmp</font><font color="#007700">)) {<br /> </font><font color="#FF8000">// if the execute or fetch fails, initialize to empty array<br /> </font><font color="#0000BB">$session_data </font><font color="#007700">= array();<br />} else {<br /> </font><font color="#FF8000">// we should now have the serialized data in $tmp[0].<br /> </font><font color="#0000BB">$session_data </font><font color="#007700">= </font><font color="#0000BB">unserialize</font><font color="#007700">(</font><font color="#0000BB">$tmp</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]);<br /> if (!</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$session_data</font><font color="#007700">)) {<br /> </font><font color="#FF8000">// something went wrong, initialize to empty array<br /> </font><font color="#0000BB">$session_data </font><font color="#007700">= array();<br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.serialize.html" ><B CLASS="function" >serialize()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.strval.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.unset.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >strval</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.variables.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >unset</TD ></TR ></TABLE ></DIV ></BODY ></HTML >