<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/reserved.interfaces.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'class.serializable.php',
    1 => 'Serializable',
    2 => 'L\'interface Serializable',
  ),
  'up' => 
  array (
    0 => 'reserved.interfaces.php',
    1 => 'Interfaces et Classes Pr&eacute;d&eacute;finies',
  ),
  'prev' => 
  array (
    0 => 'arrayaccess.offsetunset.php',
    1 => 'ArrayAccess::offsetUnset',
  ),
  'next' => 
  array (
    0 => 'serializable.serialize.php',
    1 => 'Serializable::serialize',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/predefined/serializable.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.serializable.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.serializable" class="reference">

 <h1 class="title">L&#039;interface Serializable</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p>


  <div class="section" id="serializable.intro">
   <h2 class="title">Introduction</h2>
   <p class="para">
    Interface permettant de personnaliser la sérialisation.
   </p>

   <p class="para">
    Les classes implémentant cette interface ne supportent plus
    <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> et
    <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a>.
    La méthode de sérialisation est appelée chaque fois qu&#039;une
    instance doit être sérialisée. Elle n&#039;appelle pas la méthode
    __destruct() et n&#039;a aucun effet sur le contenu de cette méthode.
    Lorsque les données sont sérialisées, la classe est connue et
    la méthode unserialize() appropriée est appelée comme constructeur
    au lieu d&#039;appeler __construct(). S&#039;il est nécessaire d&#039;appeler le constructeur
    standard, il est possible de le faire dans la méthode.
   </p>

   <div class="warning"><strong class="warning">Avertissement</strong>
    <p class="para">
     À partir de PHP 8.1.0, une classe qui implémente <span class="interfacename"><strong class="interfacename">Serializable</strong></span>
     sans aussi implémenter <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a>
     et <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a> génèrera
     une notice de dépréciation.
    </p>
   </div>
  </div>


  <div class="section" id="serializable.synopsis">
   <h2 class="title">Sommaire de l&#039;Interface</h2>


   <div class="classsynopsis"><div class="classsynopsisinfo">
    
     <span class="modifier">interface</span> <strong class="interfacename"><strong class="interfacename">Serializable</strong></strong> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Méthodes */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="serializable.serialize.php" class="methodname">serialize</a></span>(): <span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="serializable.unserialize.php" class="methodname">unserialize</a></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$data</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

   }</div>


  </div>

  <div class="section" id="serializable.examples">
   <h2 class="title">Exemples</h2>
   <div class="example" id="serializable.example.basic">
    <p><strong>Exemple #1 Exemple simple</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">-&gt;</span><span style="color: #0000BB">data </span><span style="color: #007700">= </span><span style="color: #DD0000">"Mes données privées"</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">-&gt;</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">-&gt;</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">-&gt;</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">-&gt;</span><span style="color: #0000BB">getData</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(44) &quot;C:3:&quot;obj&quot;:29:{s:21:&quot;Mes données privées&quot;;}&quot;
string(21) &quot;Mes données privées&quot;
</pre></div>
    </div>
   </div>
  </div>


 </div>

 





 






<h2>Sommaire</h2><ul class="chunklist chunklist_reference"><li><a href="serializable.serialize.php">Serializable::serialize</a> — Repr&eacute;sentation lin&eacute;aire de l'objet</li><li><a href="serializable.unserialize.php">Serializable::unserialize</a> — Construit un objet</li></ul>
</div>
<?php manual_footer($setup); ?>