Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2983

php-manual-en-4.3.0-2mdk.noarch.rpm

<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 Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Classes and Objects"
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 Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="keyword.parent.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 14. Classes and Objects</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.oop.magic-functions.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop.serialization"
></A
>Serializing objects - objects in sessions</H1
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </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
>Caution</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;    The following information is valid for PHP 4 only. 
   </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;   <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
>&#13;   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 $a 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 $a. If
   you want to be able to unserialize this on page2.php,
   recreating $a of class A, the definition of class A must
   be present in page2.php. This can be done for example
   by storing the class defintion of class A in an include
   file and including this file in both page1.php and page2.php.
  </P
><DIV
CLASS="informalexample"
><A
NAME="AEN5548"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>classa.inc:
  class A 
  {
      var $one = 1;
    
      function show_one()
      {
          echo $this-&#62;one;
      }
  }
  
page1.php:
  include("classa.inc");
  
  $a = new A;
  $s = serialize($a);
  // store $s somewhere where page2.php can find it.
  $fp = fopen("store", "w");
  fputs($fp, $s);
  fclose($fp);

page2.php:
  // this is needed for the unserialize to work properly.
  include("classa.inc");

  $s = implode("", @file("store"));
  $a = unserialize($s);

  // now use the function show_one() of the $a object.  
  $a-&#62;show_one();</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;   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
>&#13;   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
   <TT
CLASS="literal"
>stdClass</TT
> without any functions available
   at all, that is, it will become quite useless.
  </P
><P
>&#13;   So if in the example above $a became part of a session by
   running <TT
CLASS="literal"
>session_register("a")</TT
>, you should
   include the file <TT
CLASS="literal"
>classa.inc</TT
> 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"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.oop.magic-functions.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><TT
CLASS="literal"
>parent</TT
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>The magic functions <TT
CLASS="literal"
>__sleep</TT
> and <TT
CLASS="literal"
>__wakeup</TT
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>