<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'class.serializable.php',
    1 => 'Serializable',
    2 => 'The Serializable interface',
  ),
  'up' => 
  array (
    0 => 'reserved.interfaces.php',
    1 => 'Predefined Interfaces and Classes',
  ),
  'prev' => 
  array (
    0 => 'arrayaccess.offsetunset.php',
    1 => 'ArrayAccess::offsetUnset',
  ),
  'next' => 
  array (
    0 => 'serializable.serialize.php',
    1 => 'Serializable::serialize',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">The Serializable interface</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">Вступ</h2>
   <p class="para">
    Interface for customized serializing.
   </p>

   <p class="simpara">
    Classes that implement this interface no longer support
    <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>.
    The <span class="methodname"><a href="function.serialize.php" class="methodname">serialize()</a></span> method is
    called whenever an instance needs to be serialized.
    This does not invoke <span class="methodname"><strong>__destruct()</strong></span>
    or have any other side effect unless programmed inside the method.
    When the data is unserialized the class is known and the appropriate
    <span class="methodname"><a href="function.unserialize.php" class="methodname">unserialize()</a></span> method is called as
    a constructor instead of calling <span class="methodname"><strong>__construct()</strong></span>.
    The constructor can be called from within the
    <span class="methodname"><a href="function.unserialize.php" class="methodname">unserialize()</a></span> method if desired.
   </p>
   
   <div class="warning"><strong class="warning">Увага</strong>
    <p class="para">
     As of PHP 8.1.0, a class which implements <span class="interfacename"><strong class="interfacename">Serializable</strong></span> without also implementing <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> and <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a> will generate a deprecation warning.
    </p>
   </div>

   <p class="simpara">
    New code should use the
    <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> and
    <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a> magic methods
    instead, available as of PHP 7.4.0.
    When a class declares both them and this interface,
    <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> always uses
    <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> and never reaches
    <span class="methodname"><a href="serializable.serialize.php" class="methodname">Serializable::serialize()</a></span>.
    Unserializing is decided by the format of the data instead: a payload
    written by PHP 7.4.0 or later is read by
    <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a>, one written
    before it is still read by
    <span class="methodname"><a href="serializable.unserialize.php" class="methodname">Serializable::unserialize()</a></span>.
    Implementing <span class="interfacename"><strong class="interfacename">Serializable</strong></span> therefore remains
    of use for reading such older payloads, and for satisfying a
    <span class="interfacename"><strong class="interfacename">Serializable</strong></span> type declaration; declaring all
    four methods covers every case, and avoids the deprecation notice.
   </p>

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <p class="simpara">
     The magic methods have no interface of their own, so a class which
     declares them without implementing
     <span class="interfacename"><strong class="interfacename">Serializable</strong></span> is not an instance of it.
     <span class="function"><a href="function.method-exists.php" class="function">method_exists()</a></span> is the way to detect them.
    </p>
   </p></blockquote>
  </div>

  <div class="section" id="serializable.synopsis">
   <h2 class="title">Короткий огляд iнтерфейcу</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">/* Методи */</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">Приклади</h2>
   <div class="example" id="serializable.example.basic">
    <p><strong>Приклад #1 Supporting PHP 7.1.0 through 7.3.0</strong></p>
    <div class="example-contents"><p>
     The magic methods carry the serialized form, and the interface methods
     delegate to them, so that a single representation serves both.
    </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">// Never called as of 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">// Still called as of PHP 7.4.0, for data written before it
    </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>Поданий вище приклад
виведе:</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>
     Prior to PHP 7.4.0, the same code serializes through the interface
     instead, and outputs
     <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>Зміст</h2><ul class="chunklist chunklist_reference"><li><a href="serializable.serialize.php">Serializable::serialize</a> — String representation of object</li><li><a href="serializable.unserialize.php">Serializable::unserialize</a> — Constructs the object</li></ul>
</div>
<?php manual_footer($setup); ?>