<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Binds and calls the closure</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="closure.bindto.html">Closure::bindTo</a></div> <div class="next" style="text-align: right; float: right;"><a href="closure.fromcallable.html">Closure::fromCallable</a></div> <div class="up"><a href="class.closure.html">Closure</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="closure.call" class="refentry"> <div class="refnamediv"> <h1 class="refname">Closure::call</h1> <p class="verinfo">(PHP 7)</p><p class="refpurpose"><span class="refname">Closure::call</span> — <span class="dc-title">Binds and calls the closure</span></p> </div> <div class="refsect1 description" id="refsect1-closure.call-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>Closure::call</strong></span> ( <span class="methodparam"><span class="type">object</span> <code class="parameter">$newthis</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span> ] )</div> <p class="para rdfs-comment"> Temporarily binds the closure to <code class="parameter">newthis</code>, and calls it with any given parameters. </p> </div> <div class="refsect1 parameters" id="refsect1-closure.call-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">newthis</code></dt> <dd> <p class="para"> The <span class="type"><a href="language.types.object.html" class="type object">object</a></span> to bind the closure to for the duration of the call. </p> </dd> <dt> <code class="parameter">...</code></dt> <dd> <p class="para"> Zero or more parameters, which will be given as parameters to the closure. </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-closure.call-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the return value of the closure. </p> </div> <div class="refsect1 examples" id="refsect1-closure.call-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-330"> <p><strong>Example #1 <span class="function"><strong>Closure::call()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Value </span><span style="color: #007700">{<br /> protected </span><span style="color: #0000BB">$value</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">$value</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">value </span><span style="color: #007700">= </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br /> }<br /><br /> public function </span><span style="color: #0000BB">getValue</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">value</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$three </span><span style="color: #007700">= new </span><span style="color: #0000BB">Value</span><span style="color: #007700">(</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$four </span><span style="color: #007700">= new </span><span style="color: #0000BB">Value</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$closure </span><span style="color: #007700">= function (</span><span style="color: #0000BB">$delta</span><span style="color: #007700">) { </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getValue</span><span style="color: #007700">() + </span><span style="color: #0000BB">$delta</span><span style="color: #007700">); };<br /></span><span style="color: #0000BB">$closure</span><span style="color: #007700">-></span><span style="color: #0000BB">call</span><span style="color: #007700">(</span><span style="color: #0000BB">$three</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$closure</span><span style="color: #007700">-></span><span style="color: #0000BB">call</span><span style="color: #007700">(</span><span style="color: #0000BB">$four</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> int(7) int(8) </pre></div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="closure.bindto.html">Closure::bindTo</a></div> <div class="next" style="text-align: right; float: right;"><a href="closure.fromcallable.html">Closure::fromCallable</a></div> <div class="up"><a href="class.closure.html">Closure</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>