<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.oop5.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.oop5.serialization.php',
    1 => 'Object Serialization',
    2 => 'Serializing objects - objects in sessions',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Classes and Objects',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.references.php',
    1 => 'Objects and references',
  ),
  'next' => 
  array (
    0 => 'language.oop5.variance.php',
    1 => 'Covariance and Contravariance',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/oop5/serialization.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.oop5.serialization" class="sect1">
  <h2 class="title">Serializing objects - objects in sessions</h2>
  

  <p class="para">
   <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> returns a string containing a
   byte-stream representation of any value that can be stored in
   PHP. <span class="function"><a href="function.unserialize.php" class="function">unserialize()</a></span> can use this string to
   recreate the original variable values. Using serialize to
   save an object will save all variables in an object.  The
   methods in an object will not be saved, only the name of
   the class.
  </p>
  
  <p class="para">
   In order to be able to <span class="function"><a href="function.unserialize.php" class="function">unserialize()</a></span> an object, the
   class of that object needs to be defined. That is, if you have an object
    of class A and serialize this, you&#039;ll
   get a string that refers to class A and contains all values of variables
   contained in it. If you want to be able to unserialize
   this in another file, an object of class A, the
   definition of class A must be present in that file first.
   This can be done for example by storing the class definition of class A
   in an include file and including this file or making use of the
   <span class="function"><a href="function.spl-autoload-register.php" class="function">spl_autoload_register()</a></span> function.
  </p>
  
  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// A.php:<br />  <br />  </span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br />      public </span><span style="color: #0000BB">$one </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />    <br />      public function </span><span style="color: #0000BB">show_one</span><span style="color: #007700">() {<br />          echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">one</span><span style="color: #007700">;<br />      }<br />  }<br />  <br /></span><span style="color: #FF8000">// page1.php:<br /><br />  </span><span style="color: #007700">include </span><span style="color: #DD0000">"A.php"</span><span style="color: #007700">;<br />  <br />  </span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br />  </span><span style="color: #0000BB">$s </span><span style="color: #007700">= </span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />  </span><span style="color: #FF8000">// store $s somewhere where page2.php can find it.<br />  </span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'store'</span><span style="color: #007700">, </span><span style="color: #0000BB">$s</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// page2.php:<br />  <br />  // this is needed for the unserialize to work properly.<br />  </span><span style="color: #007700">include </span><span style="color: #DD0000">"A.php"</span><span style="color: #007700">;<br /><br />  </span><span style="color: #0000BB">$s </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'store'</span><span style="color: #007700">);<br />  </span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">);<br /><br />  </span><span style="color: #FF8000">// now use the function show_one() of the $a object.  <br />  </span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">show_one</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   It is strongly recommended that if an application serializes objects, for use
   later in the application, that the application includes the class definition
   for that object throughout the application. Not doing so might result in an
   object being unserialized without a class definition, which will result in
   PHP giving the object a class of <span class="classname"><strong class="classname">__PHP_Incomplete_Class_Name</strong></span>,
   which has no methods and would render the object useless.
  </p>
  
  <p class="para">
   So if in the example above <var class="varname">$a</var> became part of a session
   by adding a new key to the <var class="varname"><a href="reserved.variables.session.php" class="classname">$_SESSION</a></var> superglobal array, you should include the
   file <code class="literal">A.php</code> on all of your pages, not only <var class="filename">page1.php</var>
   and <var class="filename">page2.php</var>.
  </p>

  <p class="para">
   Beyond the above advice, note that you can also hook into the serialization
   and unserialization events on an object using the
   <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> and
   <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a> methods. Using
   <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> also allows you to only
   serialize a subset of the object&#039;s properties.
  </p>
 </div><?php manual_footer($setup); ?>