Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8931

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Object Serialization</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.references.html">Objects and references</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.oop5.changelog.html">OOP Changelog</a></div>
 <div class="up"><a href="language.oop5.html">Classes and Objects</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.oop5.serialization" class="sect1">
  <h2 class="title">Object Serialization</h2>
  <h2 class="title">Serializing objects - objects in sessions</h2>

  <p class="para">
    <span class="function"><a href="function.serialize.html" 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.html" 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.html" 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.html" class="function">spl_autoload_register()</a></span> function.
  </p>
  
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;classa.inc:<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$one&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">show_one</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">one</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;<br /></span><span style="color: #FF8000">//&nbsp;page1.php:<br /><br />&nbsp;&nbsp;</span><span style="color: #007700">include(</span><span style="color: #DD0000">"classa.inc"</span><span style="color: #007700">);<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;store&nbsp;$s&nbsp;somewhere&nbsp;where&nbsp;page2.php&nbsp;can&nbsp;find&nbsp;it.<br />&nbsp;&nbsp;</span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'store'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;page2.php:<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;//&nbsp;this&nbsp;is&nbsp;needed&nbsp;for&nbsp;the&nbsp;unserialize&nbsp;to&nbsp;work&nbsp;properly.<br />&nbsp;&nbsp;</span><span style="color: #007700">include(</span><span style="color: #DD0000">"classa.inc"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;</span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;</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 />&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$s</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;now&nbsp;use&nbsp;the&nbsp;function&nbsp;show_one()&nbsp;of&nbsp;the&nbsp;$a&nbsp;object.&nbsp;&nbsp;<br />&nbsp;&nbsp;</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">
   If an application is using sessions and uses 
    <span class="function"><a href="function.session-register.html" class="function">session_register()</a></span> 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 means that 
   these objects can show up on any of the application&#039;s pages once they become 
   part of the session. However, the  <span class="function"><a href="function.session-register.html" class="function">session_register()</a></span> is
   removed since PHP 5.4.0.
  </p>
  
  <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 <strong class="classname">__PHP_Incomplete_Class_Name</strong>,
   which has no methods and would render the object useless.
  </p>
  
  <p class="para">
   So if in the example above <var class="varname"><var class="varname">$a</var></var> became part of a session
   by running <em>session_register(&quot;a&quot;)</em>, you should include the
   file <em>classa.inc</em> 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.html#object.sleep" class="link">__sleep()</a> and
   <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a> methods. Using
   <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> also allows you to only
   serialize a subset of the object&#039;s properties.
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.references.html">Objects and references</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.oop5.changelog.html">OOP Changelog</a></div>
 <div class="up"><a href="language.oop5.html">Classes and Objects</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>