<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Serializing objects - objects in sessions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Handleiding" HREF="index.html"><LINK REL="UP" TITLE="Classes and Objects (PHP 4)" HREF="language.oop.html"><LINK REL="PREVIOUS" TITLE="parent" HREF="keyword.parent.html"><LINK REL="NEXT" TITLE="The magic functions __sleep and __wakeup" HREF="language.oop.magic-functions.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" 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" >PHP Handleiding</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="keyword.parent.html" ACCESSKEY="P" >Terug</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Hoofdstuk 13. Classes and Objects (PHP 4)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.oop.magic-functions.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop.serialization" >Serializing objects - objects in sessions</A ></H1 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Opmerking: </B > In PHP 3, objects will lose their class association throughout the process of serialization and unserialization. The resulting variable is of type object, but has no class and no methods, thus it is pretty useless (it has become just like an array with a funny syntax). </P ></BLOCKQUOTE ></DIV ><DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Let op</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > The following information is valid for PHP 4 only. </P ></TD ></TR ></TABLE ></DIV ><P > <A HREF="function.serialize.html" ><B CLASS="function" >serialize()</B ></A > returns a string containing a byte-stream representation of any value that can be stored in PHP. <A HREF="function.unserialize.html" ><B CLASS="function" >unserialize()</B ></A > can use this string to recreate the original variable values. Using serialize to save an object will save all variables in an object. The functions in an object will not be saved, only the name of the class. </P ><P > In order to be able to <A HREF="function.unserialize.html" ><B CLASS="function" >unserialize()</B ></A > an object, the class of that object needs to be defined. That is, if you have an object <VAR CLASS="varname" >$a</VAR > of class A on page1.php and serialize this, you'll get a string that refers to class A and contains all values of variabled contained in <VAR CLASS="varname" >$a</VAR >. If you want to be able to unserialize this on page2.php, recreating <VAR CLASS="varname" >$a</VAR > of class A, the definition of class A must be present in page2.php. This can be done for example by storing the class definition of class A in an include file and including this file in both page1.php and page2.php. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4821" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// classa.inc:<br /> <br /> </font><font color="#007700">class </font><font color="#0000BB">A </font><font color="#007700">{<br /> var </font><font color="#0000BB">$one </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br /> <br /> function </font><font color="#0000BB">show_one</font><font color="#007700">() {<br /> echo </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">one</font><font color="#007700">;<br /> }<br /> }<br /> <br /></font><font color="#FF8000">// page1.php:<br /><br /> </font><font color="#007700">include(</font><font color="#DD0000">"classa.inc"</font><font color="#007700">);<br /> <br /> </font><font color="#0000BB">$a </font><font color="#007700">= new </font><font color="#0000BB">A</font><font color="#007700">;<br /> </font><font color="#0000BB">$s </font><font color="#007700">= </font><font color="#0000BB">serialize</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">);<br /> </font><font color="#FF8000">// store $s somewhere where page2.php can find it.<br /> </font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"store"</font><font color="#007700">, </font><font color="#DD0000">"w"</font><font color="#007700">);<br /> </font><font color="#0000BB">fwrite</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">, </font><font color="#0000BB">$s</font><font color="#007700">);<br /> </font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// page2.php:<br /> <br /> // this is needed for the unserialize to work properly.<br /> </font><font color="#007700">include(</font><font color="#DD0000">"classa.inc"</font><font color="#007700">);<br /><br /> </font><font color="#0000BB">$s </font><font color="#007700">= </font><font color="#0000BB">implode</font><font color="#007700">(</font><font color="#DD0000">""</font><font color="#007700">, @</font><font color="#0000BB">file</font><font color="#007700">(</font><font color="#DD0000">"store"</font><font color="#007700">));<br /> </font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">unserialize</font><font color="#007700">(</font><font color="#0000BB">$s</font><font color="#007700">);<br /><br /> </font><font color="#FF8000">// now use the function show_one() of the $a object. <br /> </font><font color="#0000BB">$a</font><font color="#007700">-></font><font color="#0000BB">show_one</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > If you are using sessions and use <A HREF="function.session-register.html" ><B CLASS="function" >session_register()</B ></A > to register objects, these objects are serialized automatically at the end of each PHP page, and are unserialized automatically on each of the following pages. This basically means that these objects can show up on any of your pages once they become part of your session. </P ><P > It is strongly recommended that you include the class definitions of all such registered objects on all of your pages, even if you do not actually use these classes on all of your pages. If you don't and an object is being unserialized without its class definition being present, it will lose its class association and become an object of class <VAR CLASS="literal" >stdClass</VAR > without any functions available at all, that is, it will become quite useless. </P ><P > So if in the example above <VAR CLASS="varname" >$a</VAR > became part of a session by running <VAR CLASS="literal" >session_register("a")</VAR >, you should include the file <VAR CLASS="literal" >classa.inc</VAR > on all of your pages, not only page1.php and page2.php. </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="keyword.parent.html" ACCESSKEY="P" >Terug</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Begin</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.oop.magic-functions.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><VAR CLASS="literal" >parent</VAR ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop.html" ACCESSKEY="U" >Omhoog</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >The magic functions <VAR CLASS="literal" >__sleep</VAR > and <VAR CLASS="literal" >__wakeup</VAR ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >