<?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="simpara">
    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 <span class="methodname"><a href="function.serialize.php" class="methodname">serialize()</a></span> est appelée chaque fois
    qu&#039;une instance doit être sérialisée.
    Elle n&#039;appelle pas <span class="methodname"><strong>__destruct()</strong></span>
    et ne produit aucun autre effet de bord, sauf si cela est codé
    dans la méthode.
    Lorsque les données sont désérialisées, la classe est connue et
    la méthode <span class="methodname"><a href="function.unserialize.php" class="methodname">unserialize()</a></span> appropriée est appelée
    comme constructeur au lieu d&#039;appeler <span class="methodname"><strong>__construct()</strong></span>.
    Le constructeur peut être appelé depuis la méthode
    <span class="methodname"><a href="function.unserialize.php" class="methodname">unserialize()</a></span> si besoin.
   </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>

   <p class="simpara">
    Le nouveau code devrait utiliser les méthodes magiques
    <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>,
    disponibles à partir de PHP 7.4.0.
    Lorsqu&#039;une classe les déclare en plus de cette interface,
    <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> utilise toujours
    <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> et n&#039;atteint jamais
    <span class="methodname"><a href="serializable.serialize.php" class="methodname">Serializable::serialize()</a></span>.
    La désérialisation est déterminée par le format des données : un flux
    écrit par PHP 7.4.0 ou ultérieur est lu par
    <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a>, un flux écrit
    avant l&#039;est toujours par
    <span class="methodname"><a href="serializable.unserialize.php" class="methodname">Serializable::unserialize()</a></span>.
    Implémenter <span class="interfacename"><strong class="interfacename">Serializable</strong></span> reste donc utile
    pour lire d&#039;anciens flux, et pour satisfaire une déclaration de type
    <span class="interfacename"><strong class="interfacename">Serializable</strong></span> ; déclarer les quatre méthodes
    couvre tous les cas et évite la notice de dépréciation.
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="simpara">
     Les méthodes magiques n&#039;ont pas d&#039;interface propre : une classe qui les
     déclare sans implémenter <span class="interfacename"><strong class="interfacename">Serializable</strong></span>
     n&#039;en est pas une instance.
     <span class="function"><a href="function.method-exists.php" class="function">method_exists()</a></span> est le moyen de les détecter.
    </p>
   </p></blockquote>
  </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="modifier">function</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="modifier">function</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 Prise en charge de PHP 7.1.0 à 7.3.0</strong></p>
    <div class="example-contents"><p>
     Les méthodes magiques portent la forme sérialisée, et les méthodes de
     l&#039;interface leur délèguent, de sorte qu&#039;une seule représentation convient
     dans les deux cas.
    </p></div>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">class </span><span style="color: #0000BB">Task </span><span style="color: #007700">implements </span><span style="color: #0000BB">Serializable
</span><span style="color: #007700">{
    private </span><span style="color: #0000BB">$label</span><span style="color: #007700">;

    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$label</span><span style="color: #007700">)
    {
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">label </span><span style="color: #007700">= </span><span style="color: #0000BB">$label</span><span style="color: #007700">;
    }

    public function </span><span style="color: #0000BB">__serialize</span><span style="color: #007700">(): array
    {
        return [</span><span style="color: #DD0000">'label' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">label</span><span style="color: #007700">];
    }

    public function </span><span style="color: #0000BB">__unserialize</span><span style="color: #007700">(array </span><span style="color: #0000BB">$data</span><span style="color: #007700">): </span><span style="color: #0000BB">void
    </span><span style="color: #007700">{
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">label </span><span style="color: #007700">= </span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'label'</span><span style="color: #007700">];
    }

    </span><span style="color: #FF8000">// Jamais appelé à partir de PHP 7.4.0
    </span><span style="color: #007700">public function </span><span style="color: #0000BB">serialize</span><span style="color: #007700">()
    {
        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">__serialize</span><span style="color: #007700">());
    }

    </span><span style="color: #FF8000">// Toujours appelé à partir de PHP 7.4.0, pour les données écrites avant
    </span><span style="color: #007700">public function </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">)
    {
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">__unserialize</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">));
    }
}

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">serialize</span><span style="color: #007700">(new </span><span style="color: #0000BB">Task</span><span style="color: #007700">(</span><span style="color: #DD0000">'deploy'</span><span style="color: #007700">)));
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
string(40) &quot;O:4:&quot;Task&quot;:1:{s:5:&quot;label&quot;;s:6:&quot;deploy&quot;;}&quot;
</pre></div>
    </div>
    <div class="example-contents"><p>
     Avant PHP 7.4.0, le même code sérialise via l&#039;interface et affiche
     <code class="literal">string(47) &quot;C:4:&quot;Task&quot;:31:{a:1:{s:5:&quot;label&quot;;s:6:&quot;deploy&quot;;}}&quot;</code>.
    </p></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); ?>