<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.oop5.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.oop5.magic.php',
    1 => 'Magic Methods',
    2 => 'Magic Methods',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Classes and Objects',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.iterations.php',
    1 => 'Object Iteration',
  ),
  'next' => 
  array (
    0 => 'language.oop5.final.php',
    1 => 'Final Keyword',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/oop5/magic.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.oop5.magic" class="sect1">
  <h2 class="title">Magic Methods</h2>
  <p class="para">
   Magic methods are special methods which override PHP&#039;s default&#039;s action
   when certain actions are performed on an object.
  </p>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="simpara">
    All methods names starting with <code class="literal">__</code> are reserved by PHP.
    Therefore, it is not recommended to use such method names unless overriding
    PHP&#039;s behavior.
   </p>
  </div>
  <p class="para">
   The following method names are considered magical:
   
   <a href="language.oop5.decon.php#object.construct" class="link">__construct()</a>,
   <a href="language.oop5.decon.php#object.destruct" class="link">__destruct()</a>,
   <a href="language.oop5.overloading.php#object.call" class="link">__call()</a>,
   <a href="language.oop5.overloading.php#object.callstatic" class="link">__callStatic()</a>,
   <a href="language.oop5.overloading.php#object.get" class="link">__get()</a>,
   <a href="language.oop5.overloading.php#object.set" class="link">__set()</a>,
   <a href="language.oop5.overloading.php#object.isset" class="link">__isset()</a>,
   <a href="language.oop5.overloading.php#object.unset" class="link">__unset()</a>,
   <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a>,
   <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a>,
   <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a>,
   <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a>,
   <a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a>,
   <a href="language.oop5.magic.php#object.invoke" class="link">__invoke()</a>,
   <a href="language.oop5.magic.php#object.set-state" class="link">__set_state()</a>,
   <a href="language.oop5.cloning.php#object.clone" class="link">__clone()</a>, and
   <a href="language.oop5.magic.php#object.debuginfo" class="link">__debugInfo()</a>.
  </p>

  <div class="warning"><strong class="warning">Warning</strong>
   
   <p class="simpara">
    All magic methods, with the exception of
    <a href="language.oop5.decon.php#object.construct" class="link">__construct()</a>,
    <a href="language.oop5.decon.php#object.destruct" class="link">__destruct()</a>, and
    <a href="language.oop5.cloning.php#object.clone" class="link">__clone()</a>,
    <em>must</em> be declared as <code class="literal">public</code>,
    otherwise an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is emitted.
    Prior to PHP 8.0.0, no diagnostic was emitted for the magic methods
    <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a>,
    <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a>,
    <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a>,
    <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a>, and
    <a href="language.oop5.magic.php#object.set-state" class="link">__set_state()</a>.
   </p>
  </div>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    If type declarations are used in the definition of a magic method, they
    must be identical to the signature described in this document.
    Otherwise, a fatal error is emitted.
    Prior to PHP 8.0.0, no diagnostic was emitted.
    However, <a href="language.oop5.decon.php#object.construct" class="link">__construct()</a> and
    <a href="language.oop5.decon.php#object.destruct" class="link">__destruct()</a> must not declare a return type;
    otherwise a fatal error is emitted.
   </p>
  </div>

  <div class="sect2" id="language.oop5.magic.serialize">
   <h3 class="title">
    <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>
   </h3>

   <div class="methodsynopsis dc-description" id="object.serialize">
    <span class="modifier">public</span> <span class="methodname"><strong>__serialize</strong></span>(): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

   <div class="methodsynopsis dc-description" id="object.unserialize"><span class="modifier">public</span> <span class="methodname"><strong>__unserialize</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$data</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>


   <p class="para">
    <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> checks if the class has a function with
    the magic name <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a>. If so, that function is
    executed prior to any serialization. It must construct and return an associative array of key/value pairs
    that represent the serialized form of the object. If no array is returned a <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span>
    will be thrown.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     If both <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> and <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a>
     are defined in the same object, only <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> will be called.
     <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> will be ignored. If the object implements the <a href="class.serializable.php" class="link">Serializable</a>
     interface, the interface&#039;s <code class="literal">serialize()</code> method will be ignored and <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a>
     used instead.
    </p>
   </p></blockquote>
   <p class="para">
    The intended use of <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> is to define a serialization-friendly
    arbitrary representation of the object. Elements of the array may correspond to properties of the object but
    that is not required.
   </p>
   <p class="para">
    Conversely, <span class="function"><a href="function.unserialize.php" class="function">unserialize()</a></span> checks for the
    presence of a function with the magic name
    <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a>. If present, this function will be passed the
    restored array that was returned from <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a>.  It may
    then restore the properties of the object from that array as appropriate.
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     If both <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a> and <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a>
     are defined in the same object, only <a href="language.oop5.magic.php#object.unserialize" class="link">__unserialize()</a> will be called.
     <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a> will be ignored.
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     This feature is available as of PHP 7.4.0.
    </p>
   </p></blockquote>
   <div class="example" id="example-1">
    <p><strong>Example #1 Serialize and unserialize</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Connection<br /></span><span style="color: #007700">{<br />    protected </span><span style="color: #0000BB">$link</span><span style="color: #007700">;<br />    private </span><span style="color: #0000BB">$dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$username</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$username</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn </span><span style="color: #007700">= </span><span style="color: #0000BB">$dsn</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password </span><span style="color: #007700">= </span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />    }<br /><br />    private function </span><span style="color: #0000BB">connect</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">link </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password</span><span style="color: #007700">);<br />    }<br /><br />    public function </span><span style="color: #0000BB">__serialize</span><span style="color: #007700">(): array<br />    {<br />        return [<br />          </span><span style="color: #DD0000">'dsn' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn</span><span style="color: #007700">,<br />          </span><span style="color: #DD0000">'user' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username</span><span style="color: #007700">,<br />          </span><span style="color: #DD0000">'pass' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password</span><span style="color: #007700">,<br />        ];<br />    }<br /><br />    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<br />    </span><span style="color: #007700">{<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn </span><span style="color: #007700">= </span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'dsn'</span><span style="color: #007700">];<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">];<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password </span><span style="color: #007700">= </span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'pass'</span><span style="color: #007700">];<br /><br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />    }<br />}</span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </div>

 <div class="sect2" id="language.oop5.magic.sleep">
  <h3 class="title">
   <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>
  </h3>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="simpara">
    This serialization mechanism is soft-deprecated as of PHP 8.5.0.
    It is maintained for backward compatibility.
    However, new and existing code should be migrated to 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.
   </p>
  </div>

  <div class="methodsynopsis dc-description" id="object.sleep">
   <span class="modifier">public</span> <span class="methodname"><strong>__sleep</strong></span>(): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <div class="methodsynopsis dc-description" id="object.wakeup"><span class="modifier">public</span> <span class="methodname"><strong>__wakeup</strong></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>


  <p class="para">
   <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> checks if the class has a function with
   the magic name <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a>. If so, that function is
   executed prior to any serialization. It can clean up the object
   and is supposed to return an array with the names of all variables
   of that object that should be serialized.
   If the method doesn&#039;t return anything then <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> is serialized and
   <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> is issued.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    It is not possible for <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> to return names of
    private properties in parent classes. Doing this will result in an
    <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> level error.
    Use <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> instead.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    As of PHP 8.0.0, returning a value which is not an array from <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> generates a warning. Previously, it generated a notice.
   </p>
  </p></blockquote>
  <p class="para">
   The intended use of <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> is to commit pending
   data or perform similar cleanup tasks. Also, the function is
   useful if a very large object doesn&#039;t need to be saved completely.
  </p>
  <p class="para">
   Conversely, <span class="function"><a href="function.unserialize.php" class="function">unserialize()</a></span> checks for the
   presence of a function with the magic name
   <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a>. If present, this function can
   reconstruct any resources that the object may have.
  </p>
  <p class="para">
   The intended use of <a href="language.oop5.magic.php#object.wakeup" class="link">__wakeup()</a> is to
   reestablish any database connections that may have been lost
   during serialization and perform other reinitialization
   tasks.
  </p>
  <div class="example" id="example-2">
   <p><strong>Example #2 Sleep and wakeup</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Connection<br /></span><span style="color: #007700">{<br />    protected </span><span style="color: #0000BB">$link</span><span style="color: #007700">;<br />    private </span><span style="color: #0000BB">$dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$username</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$username</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn </span><span style="color: #007700">= </span><span style="color: #0000BB">$dsn</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username </span><span style="color: #007700">= </span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password </span><span style="color: #007700">= </span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />    }<br /><br />    private function </span><span style="color: #0000BB">connect</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">link </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username</span><span style="color: #007700">, </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password</span><span style="color: #007700">);<br />    }<br /><br />    public function </span><span style="color: #0000BB">__sleep</span><span style="color: #007700">()<br />    {<br />        return array(</span><span style="color: #DD0000">'dsn'</span><span style="color: #007700">, </span><span style="color: #DD0000">'username'</span><span style="color: #007700">, </span><span style="color: #DD0000">'password'</span><span style="color: #007700">);<br />    }<br /><br />    public function </span><span style="color: #0000BB">__wakeup</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />    }<br />}</span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>

  <div class="sect2" id="language.oop5.magic.tostring">
   <h3 class="title"><a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a></h3>
   <div class="methodsynopsis dc-description" id="object.tostring">
    <span class="modifier">public</span> <span class="methodname"><strong>__toString</strong></span>(): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

   <p class="para">
    The <a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a> method allows a class to decide
    how it will react when it is treated like a string. For example,
    what <code class="literal">echo $obj;</code> will print.
   </p>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="para">
     As of PHP 8.0.0, the return value follows standard PHP type semantics,
     meaning it will be coerced into a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> if possible and if
     <a href="language.types.declarations.php#language.types.declarations.strict" class="link">strict typing</a>
     is disabled.
    </p>
    <p class="para">
     A <span class="interfacename"><a href="class.stringable.php" class="interfacename">Stringable</a></span> object will
     <em>not</em> be accepted by a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> type declaration if
     <a href="language.types.declarations.php#language.types.declarations.strict" class="link">strict typing</a>
     is enabled. If such behaviour is wanted the type declaration must accept
     <span class="interfacename"><a href="class.stringable.php" class="interfacename">Stringable</a></span> and <span class="type"><a href="language.types.string.php" class="type string">string</a></span> via a union type.
    </p>
    <p class="para">
     As of PHP 8.0.0, any class that contains a <a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a>
     method will also implicitly implement the <span class="interfacename"><a href="class.stringable.php" class="interfacename">Stringable</a></span> interface, and will
     thus pass type checks for that interface.  Explicitly implementing the interface anyway is
     recommended.
    </p>
    <p class="para">
     In PHP 7.4, the returned value <em>must</em> be a
     <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, otherwise an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> is thrown.
    </p>
    <p class="para">
     Prior to PHP 7.4.0, the returned value <em>must</em> be a
     <span class="type"><a href="language.types.string.php" class="type string">string</a></span>, otherwise a fatal <strong><code><a href="errorfunc.constants.php#constant.e-recoverable-error">E_RECOVERABLE_ERROR</a></code></strong>
     is emitted.
    </p>
   </div>
   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     It was not possible to throw an exception from within a
     <a href="language.oop5.magic.php#object.tostring" class="link">__toString()</a>
     method prior to PHP 7.4.0. Doing so will result in a fatal error.
    </p>
   </div>
   <div class="example" id="example-3">
    <p><strong>Example #3 Simple example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Declare a simple class<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">TestClass<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">__toString</span><span style="color: #007700">()<br />    {<br />        return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$class </span><span style="color: #007700">= new </span><span style="color: #0000BB">TestClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'Hello'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$class</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Hello
</pre></div>
    </div>
   </div>
  </div>

  <div class="sect2" id="language.oop5.magic.invoke">
   <h3 class="title"><a href="language.oop5.magic.php#object.invoke" class="link">__invoke()</a></h3>
   <div class="methodsynopsis dc-description" id="object.invoke">
    <span class="methodname"><strong>__invoke</strong></span>(<span class="methodparam"> <code class="parameter">...$values</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

   <p class="para">
    The <a href="language.oop5.magic.php#object.invoke" class="link">__invoke()</a> method is called when a script tries to
    call an object as a function.
   </p>
   <div class="example" id="example-4">
    <p><strong>Example #4 Using <a href="language.oop5.magic.php#object.invoke" class="link">__invoke()</a></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">CallableClass<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__invoke</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">);<br />    }<br />}<br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">CallableClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">is_callable</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
int(5)
bool(true)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-5">
    <p><strong>Example #5 Using <a href="language.oop5.magic.php#object.invoke" class="link">__invoke()</a></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Sort<br /></span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$key</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">string $key</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">key </span><span style="color: #007700">= </span><span style="color: #0000BB">$key</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">__invoke</span><span style="color: #007700">(array </span><span style="color: #0000BB">$a</span><span style="color: #007700">, array </span><span style="color: #0000BB">$b</span><span style="color: #007700">): </span><span style="color: #0000BB">int<br />    </span><span style="color: #007700">{<br />        return </span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">key</span><span style="color: #007700">] &lt;=&gt; </span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">key</span><span style="color: #007700">];<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$customers </span><span style="color: #007700">= [<br />    [</span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'John'</span><span style="color: #007700">, </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Do'</span><span style="color: #007700">],<br />    [</span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Alice'</span><span style="color: #007700">, </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Gustav'</span><span style="color: #007700">],<br />    [</span><span style="color: #DD0000">'id' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'first_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Bob'</span><span style="color: #007700">, </span><span style="color: #DD0000">'last_name' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'Filipe'</span><span style="color: #007700">]<br />];<br /><br /></span><span style="color: #FF8000">// sort customers by first name<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">, new </span><span style="color: #0000BB">Sort</span><span style="color: #007700">(</span><span style="color: #DD0000">'first_name'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// sort customers by last name<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">, new </span><span style="color: #0000BB">Sort</span><span style="color: #007700">(</span><span style="color: #DD0000">'last_name'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [id] =&gt; 3
            [first_name] =&gt; Alice
            [last_name] =&gt; Gustav
        )

    [1] =&gt; Array
        (
            [id] =&gt; 2
            [first_name] =&gt; Bob
            [last_name] =&gt; Filipe
        )

    [2] =&gt; Array
        (
            [id] =&gt; 1
            [first_name] =&gt; John
            [last_name] =&gt; Do
        )

)
Array
(
    [0] =&gt; Array
        (
            [id] =&gt; 1
            [first_name] =&gt; John
            [last_name] =&gt; Do
        )

    [1] =&gt; Array
        (
            [id] =&gt; 2
            [first_name] =&gt; Bob
            [last_name] =&gt; Filipe
        )

    [2] =&gt; Array
        (
            [id] =&gt; 3
            [first_name] =&gt; Alice
            [last_name] =&gt; Gustav
        )

)
</pre></div>
    </div>
   </div>
  </div>

  <div class="sect2" id="language.oop5.magic.set-state">
   <h3 class="title"><a href="language.oop5.magic.php#object.set-state" class="link">__set_state()</a></h3>
   <div class="methodsynopsis dc-description" id="object.set-state">
    <span class="modifier">static</span> <span class="methodname"><strong>__set_state</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$properties</code></span>): <span class="type"><a href="language.types.object.php" class="type object">object</a></span></div>

   <p class="para">
    This <a href="language.oop5.static.php" class="link">static</a> method is called
    for classes exported by <span class="function"><a href="function.var-export.php" class="function">var_export()</a></span>.
   </p>
   <p class="para">
    The only parameter of this method is an array containing exported
    properties in the form <code class="literal">[&#039;property&#039; =&gt; value, ...]</code>.
   </p>
   <div class="example" id="example-6">
    <p><strong>Example #6 Using <a href="language.oop5.magic.php#object.set-state" class="link">__set_state()</a></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$var1</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">$var2</span><span style="color: #007700">;<br /><br />    public static function </span><span style="color: #0000BB">__set_state</span><span style="color: #007700">(</span><span style="color: #0000BB">$an_array</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br />        </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var1 </span><span style="color: #007700">= </span><span style="color: #0000BB">$an_array</span><span style="color: #007700">[</span><span style="color: #DD0000">'var1'</span><span style="color: #007700">];<br />        </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var2 </span><span style="color: #007700">= </span><span style="color: #0000BB">$an_array</span><span style="color: #007700">[</span><span style="color: #DD0000">'var2'</span><span style="color: #007700">];<br />        return </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var1 </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var2 </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br />eval(</span><span style="color: #DD0000">'$c = ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$b </span><span style="color: #007700">. </span><span style="color: #DD0000">';'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
string(60) &quot;A::__set_state(array(
   &#039;var1&#039; =&gt; 5,
   &#039;var2&#039; =&gt; &#039;foo&#039;,
))&quot;
object(A)#2 (2) {
  [&quot;var1&quot;]=&gt;
  int(5)
  [&quot;var2&quot;]=&gt;
  string(3) &quot;foo&quot;
}
</pre></div>
    </div>
   </div>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     When exporting an object, <span class="function"><a href="function.var-export.php" class="function">var_export()</a></span> does not check
     whether <a href="language.oop5.magic.php#object.set-state" class="link">__set_state()</a> is
     implemented by the object&#039;s class, so re-importing objects will result in an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> exception,
     if __set_state() is not implemented. Particularly, this affects some
     internal classes.
    </span>
    <span class="simpara">
     It is the responsibility of the programmer to verify that only objects will
     be re-imported, whose class implements __set_state().
    </span>
   </p></blockquote>
  </div>

  <div class="sect2" id="language.oop5.magic.debuginfo">
   <h3 class="title"><a href="language.oop5.magic.php#object.debuginfo" class="link">__debugInfo()</a></h3>
   <div class="methodsynopsis dc-description" id="object.debuginfo">
    <span class="methodname"><strong>__debugInfo</strong></span>(): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

   <p class="para">
    This method is called by <span class="function"><a href="function.var-dump.php" class="function">var_dump()</a></span> when dumping an
    object to get the properties that should be shown. If the method isn&#039;t
    defined on an object, then all public, protected and private properties
    will be shown.
   </p>
   <div class="example" id="example-7">
    <p><strong>Example #7 Using <a href="language.oop5.magic.php#object.debuginfo" class="link">__debugInfo()</a></strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">C </span><span style="color: #007700">{<br />    private </span><span style="color: #0000BB">$prop</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prop </span><span style="color: #007700">= </span><span style="color: #0000BB">$val</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">__debugInfo</span><span style="color: #007700">() {<br />        return [<br />            </span><span style="color: #DD0000">'propSquared' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prop </span><span style="color: #007700">** </span><span style="color: #0000BB">2</span><span style="color: #007700">,<br />        ];<br />    }<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new </span><span style="color: #0000BB">C</span><span style="color: #007700">(</span><span style="color: #0000BB">42</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
object(C)#1 (1) {
  [&quot;propSquared&quot;]=&gt;
  int(1764)
}
</pre></div>
    </div>
   </div>
  </div>
 </div><?php manual_footer($setup); ?>