<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.closure.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'closure.bindto.php',
    1 => 'Closure::bindTo',
    2 => 'Duplicates the closure with a new bound object and class scope',
  ),
  'up' => 
  array (
    0 => 'class.closure.php',
    1 => 'Closure',
  ),
  'prev' => 
  array (
    0 => 'closure.bind.php',
    1 => 'Closure::bind',
  ),
  'next' => 
  array (
    0 => 'closure.call.php',
    1 => 'Closure::call',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/predefined/closure/bindto.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="closure.bindto" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Closure::bindTo</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">Closure::bindTo</span> &mdash; <span class="dc-title">
   Duplicates the closure with a new bound object and class scope
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-closure.bindto-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Closure::bindTo</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.object.php" class="type object">object</a></span></span> <code class="parameter">$newThis</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.object.php" class="type object">object</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$newScope</code><span class="initializer"> = &quot;static&quot;</span></span>): <span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="class.closure.php" class="type Closure">Closure</a></span></span></div>

  <p class="para rdfs-comment">
   Create and return a new <a href="functions.anonymous.php" class="link">anonymous
   function</a> with the same body and bound variables as this one, but
   possibly with a different bound object and a new class scope.
  </p>

  <p class="para">
   The “bound object” determines the value <code class="literal">$this</code> will
   have in the function body and the “class scope” represents a class
   which determines which private and protected members the anonymous
   function will be able to access. Namely, the members that will be
   visible are the same as if the anonymous function were a method of
   the class given as value of the <code class="parameter">newScope</code>
   parameter.
  </p>

  <p class="para">
   Static closures cannot have any bound object (the value of the parameter
   <code class="parameter">newThis</code> should be <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>), but this method can
   nevertheless be used to change their class scope.
  </p>

  <p class="para">
   This method will ensure that for a non-static closure, having a bound
   instance will imply being scoped and vice-versa. To this end,
   non-static closures that are given a scope but a <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> instance are made
   static and non-static non-scoped closures that are given a non-null
   instance are scoped to an unspecified class.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If you only want to duplicate the anonymous functions, you can use
    <a href="language.oop5.cloning.php" class="link">cloning</a> instead.
   </p>
  </p></blockquote>

 </div>


 <div class="refsect1 parameters" id="refsect1-closure.bindto-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   
    <dt><code class="parameter">newThis</code></dt>
    <dd>
     <p class="para">
      The object to which the given anonymous function should be bound, or
      <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> for the closure to be unbound. 
     </p>
    </dd>
   
   
    <dt><code class="parameter">newScope</code></dt>
    <dd>
     <p class="para">
      The class scope to which the closure is to be associated, or
      &#039;static&#039; to keep the current one. If an object is given, the type of the
      object will be used instead. This determines the visibility of protected
      and private methods of the bound object.
      It is not allowed to pass (an object of) an internal class as this parameter.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-closure.bindto-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the newly created <span class="classname"><a href="class.closure.php" class="classname">Closure</a></span> object
   or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-closure.bindto-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="function"><strong>Closure::bindTo()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="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 />    private </span><span style="color: #0000BB">$val</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 />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">val </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">getClosure</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #FF8000">// Returns closure bound to this object and scope<br />        </span><span style="color: #007700">return function() {<br />            return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">val</span><span style="color: #007700">;<br />        };<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$ob1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$ob2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$cl </span><span style="color: #007700">= </span><span style="color: #0000BB">$ob1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getClosure</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">$cl</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$cl </span><span style="color: #007700">= </span><span style="color: #0000BB">$cl</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindTo</span><span style="color: #007700">(</span><span style="color: #0000BB">$ob2</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$cl</span><span style="color: #007700">(), </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</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="examplescode"><pre class="examplescode">1
2</pre>
</div>
   </div>
  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-closure.bindto-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li><a href="functions.anonymous.php" class="link">Anonymous functions</a></li>
   <li><span class="methodname"><a href="closure.bind.php" class="methodname" rel="rdfs-seeAlso">Closure::bind()</a> - Duplicates a closure with a specific bound object and class scope</span></li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>