<!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>The Serializable interface</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="arrayaccess.offsetunset.html">ArrayAccess::offsetUnset</a></div> <div class="next" style="text-align: right; float: right;"><a href="serializable.serialize.html">Serializable::serialize</a></div> <div class="up"><a href="reserved.interfaces.html">Predefined Interfaces and Classes</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="class.serializable" class="reference"> <h1 class="title">The Serializable interface</h1> <div class="partintro"><p class="verinfo">(PHP 5 >= 5.1.0)</p> <div class="section" id="serializable.intro"> <h2 class="title">Introduction</h2> <p class="para"> Interface for customized serializing. </p> <p class="para"> Classes that implement this interface no longer support <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>. The method serialize is called whenever an instance needs to be serialized. This does not invoke __destruct() or has any other side effect unless programmed inside the method. When the data is unserialized the class is known and the appropriate unserialize() method is called as a constructor instead of calling __construct(). If you need to execute the standard constructor you may do so in the method. </p> </div> <div class="section" id="serializable.synopsis"> <h2 class="title">Interface synopsis</h2> <div class="classsynopsis"> <div class="ooclass"></div> <div class="classsynopsisinfo"> <span class="ooclass"> <strong class="classname">Serializable</strong> </span> {</div> <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><a href="serializable.serialize.html" class="methodname">serialize</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type">void</span> <span class="methodname"><a href="serializable.unserialize.html" class="methodname">unserialize</a></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$serialized</code></span> )</div> }</div> </div> <div class="section" id="serializable.examples"> <div class="example" id="serializable.example.basic"> <p><strong>Example #1 Basic usage</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">obj </span><span style="color: #007700">implements </span><span style="color: #0000BB">Serializable </span><span style="color: #007700">{<br /> private </span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">data </span><span style="color: #007700">= </span><span style="color: #DD0000">"My private data"</span><span style="color: #007700">;<br /> }<br /> public function </span><span style="color: #0000BB">serialize</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">data</span><span style="color: #007700">);<br /> }<br /> public function </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">data </span><span style="color: #007700">= </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /> }<br /> public function </span><span style="color: #0000BB">getData</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">data</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">obj</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ser </span><span style="color: #007700">= </span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$ser</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$newobj </span><span style="color: #007700">= </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$ser</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$newobj</span><span style="color: #007700">-></span><span style="color: #0000BB">getData</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> string(38) "C:3:"obj":23:{s:15:"My private data";}" string(15) "My private data" </pre></div> </div> </div> </div> </div> <h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="serializable.serialize.html">Serializable::serialize</a> — String representation of object</li><li><a href="serializable.unserialize.html">Serializable::unserialize</a> — Constructs the object</li></ul> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="arrayaccess.offsetunset.html">ArrayAccess::offsetUnset</a></div> <div class="next" style="text-align: right; float: right;"><a href="serializable.serialize.html">Serializable::serialize</a></div> <div class="up"><a href="reserved.interfaces.html">Predefined Interfaces and Classes</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>