<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.functions.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'functions.anonymous.php',
    1 => 'Anonymous functions',
    2 => 'Anonymous functions',
  ),
  'up' => 
  array (
    0 => 'language.functions.php',
    1 => 'Functions',
  ),
  'prev' => 
  array (
    0 => 'functions.internal.php',
    1 => 'Internal (built-in) functions',
  ),
  'next' => 
  array (
    0 => 'functions.arrow.php',
    1 => 'Arrow Functions',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/functions.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="functions.anonymous" class="sect1">
   <h2 class="title">Anonymous functions</h2>

   <p class="simpara">
    Anonymous functions, also known as <code class="literal">closures</code>, allow the
    creation of functions which have no specified name. They are most useful as
    the value of <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>
    parameters, but they have many other uses.
   </p>
   <p class="simpara">
    Anonymous functions are implemented using the <a href="class.closure.php" class="link">
    <span class="classname"><a href="class.closure.php" class="classname">Closure</a></span></a> class.
   </p>

   <div class="example" id="example-1">
    <p><strong>Приклад #1 Anonymous function example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(</span><span style="color: #DD0000">'~-([a-z])~'</span><span style="color: #007700">, function (</span><span style="color: #0000BB">$match</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$match</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br />}, </span><span style="color: #DD0000">'hello-world'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// outputs helloWorld<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="simpara">
    Closures can also be used as the values of variables; PHP automatically 
    converts such expressions into instances of the
    <span class="classname"><a href="class.closure.php" class="classname">Closure</a></span> internal class. Assigning a closure to a
    variable uses the same syntax as any other assignment, including the
    trailing semicolon:
   </p>

   <div class="example" id="example-2">
    <p><strong>Приклад #2 Anonymous function variable assignment example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$greet </span><span style="color: #007700">= function(</span><span style="color: #0000BB">$name</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hello %s\r\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br />};<br /><br /></span><span style="color: #0000BB">$greet</span><span style="color: #007700">(</span><span style="color: #DD0000">'World'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$greet</span><span style="color: #007700">(</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   
   <p class="simpara">
    Closures may also inherit variables from the parent scope. Any such
    variables must be passed to the <code class="literal">use</code> language construct.
    As of PHP 7.1, these variables must not include <a href="language.variables.predefined.php" class="link">superglobals</a>,
    <var class="varname">$this</var>, or variables with the same name as a parameter.
    A return type declaration of the function has to be placed
    <em>after</em> the <code class="literal">use</code> clause.
   </p>

   <div class="example" id="example-3">
    <p><strong>Приклад #3 Inheriting variables from the parent scope</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$message </span><span style="color: #007700">= </span><span style="color: #DD0000">'hello'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// No "use"<br /></span><span style="color: #0000BB">$example </span><span style="color: #007700">= function () {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />};<br /></span><span style="color: #0000BB">$example</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Inherit $message<br /></span><span style="color: #0000BB">$example </span><span style="color: #007700">= function () use (</span><span style="color: #0000BB">$message</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />};<br /></span><span style="color: #0000BB">$example</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Inherited variable's value is from when the function<br />// is defined, not when called<br /></span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #DD0000">'world'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$example</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Reset message<br /></span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #DD0000">'hello'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Inherit by-reference<br /></span><span style="color: #0000BB">$example </span><span style="color: #007700">= function () use (&amp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />};<br /></span><span style="color: #0000BB">$example</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// The changed value in the parent scope<br />// is reflected inside the function call<br /></span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #DD0000">'world'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$example</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Closures can also accept regular arguments<br /></span><span style="color: #0000BB">$example </span><span style="color: #007700">= function (</span><span style="color: #0000BB">$arg</span><span style="color: #007700">) use (</span><span style="color: #0000BB">$message</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg </span><span style="color: #007700">. </span><span style="color: #DD0000">' ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />};<br /></span><span style="color: #0000BB">$example</span><span style="color: #007700">(</span><span style="color: #DD0000">"hello"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Return type declaration comes after the use clause<br /></span><span style="color: #0000BB">$example </span><span style="color: #007700">= function () use (</span><span style="color: #0000BB">$message</span><span style="color: #007700">): </span><span style="color: #0000BB">string </span><span style="color: #007700">{<br />    return </span><span style="color: #DD0000">"hello </span><span style="color: #0000BB">$message</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />};<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$example</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад виведе щось
схоже на:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Notice: Undefined variable: message in /example.php on line 6
NULL
string(5) &quot;hello&quot;
string(5) &quot;hello&quot;
string(5) &quot;hello&quot;
string(5) &quot;world&quot;
string(11) &quot;hello world&quot;
string(11) &quot;hello world&quot;
</pre></div>
    </div>
   </div>

   <p class="para">
    As of PHP 8.0.0, the list of scope-inherited variables may include a trailing
    comma, which will be ignored.
   </p>
   <p class="simpara">
    Inheriting variables from the parent scope is <em>not</em> 
    the same as using global variables. 
    Global variables exist in the global scope, which is the same no
    matter what function is executing. The parent scope of a closure is the
    function in which the closure was declared (not necessarily the function it
    was called from). See the following example:
   </p>

   <div class="example" id="example-4">
    <p><strong>Приклад #4 Closures and scoping</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// A basic shopping cart which contains a list of added products<br />// and the quantity of each product. Includes a method which<br />// calculates the total price of the items in the cart using a<br />// closure as a callback.<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Cart<br /></span><span style="color: #007700">{<br />    const </span><span style="color: #0000BB">PRICE_BUTTER  </span><span style="color: #007700">= </span><span style="color: #0000BB">1.00</span><span style="color: #007700">;<br />    const </span><span style="color: #0000BB">PRICE_MILK    </span><span style="color: #007700">= </span><span style="color: #0000BB">3.00</span><span style="color: #007700">;<br />    const </span><span style="color: #0000BB">PRICE_EGGS    </span><span style="color: #007700">= </span><span style="color: #0000BB">6.95</span><span style="color: #007700">;<br /><br />    protected </span><span style="color: #0000BB">$products </span><span style="color: #007700">= array();<br />    <br />    public function </span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #0000BB">$product</span><span style="color: #007700">, </span><span style="color: #0000BB">$quantity</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">products</span><span style="color: #007700">[</span><span style="color: #0000BB">$product</span><span style="color: #007700">] = </span><span style="color: #0000BB">$quantity</span><span style="color: #007700">;<br />    }<br />    <br />    public function </span><span style="color: #0000BB">getQuantity</span><span style="color: #007700">(</span><span style="color: #0000BB">$product</span><span style="color: #007700">)<br />    {<br />        return isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">products</span><span style="color: #007700">[</span><span style="color: #0000BB">$product</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">products</span><span style="color: #007700">[</span><span style="color: #0000BB">$product</span><span style="color: #007700">] :<br />               </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">;<br />    }<br />    <br />    public function </span><span style="color: #0000BB">getTotal</span><span style="color: #007700">(</span><span style="color: #0000BB">$tax</span><span style="color: #007700">)<br />    {<br />        </span><span style="color: #0000BB">$total </span><span style="color: #007700">= </span><span style="color: #0000BB">0.00</span><span style="color: #007700">;<br />        <br />        </span><span style="color: #0000BB">$callback </span><span style="color: #007700">=<br />            function (</span><span style="color: #0000BB">$quantity</span><span style="color: #007700">, </span><span style="color: #0000BB">$product</span><span style="color: #007700">) use (</span><span style="color: #0000BB">$tax</span><span style="color: #007700">, &amp;</span><span style="color: #0000BB">$total</span><span style="color: #007700">)<br />            {<br />                </span><span style="color: #0000BB">$pricePerItem </span><span style="color: #007700">= </span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #0000BB">__CLASS__ </span><span style="color: #007700">. </span><span style="color: #DD0000">"::PRICE_" </span><span style="color: #007700">.<br />                    </span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$product</span><span style="color: #007700">));<br />                </span><span style="color: #0000BB">$total </span><span style="color: #007700">+= (</span><span style="color: #0000BB">$pricePerItem </span><span style="color: #007700">* </span><span style="color: #0000BB">$quantity</span><span style="color: #007700">) * (</span><span style="color: #0000BB">$tax </span><span style="color: #007700">+ </span><span style="color: #0000BB">1.0</span><span style="color: #007700">);<br />            };<br />        <br />        </span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">products</span><span style="color: #007700">, </span><span style="color: #0000BB">$callback</span><span style="color: #007700">);<br />        return </span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">$total</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$my_cart </span><span style="color: #007700">= new </span><span style="color: #0000BB">Cart</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Add some items to the cart<br /></span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'butter'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'milk'</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'eggs'</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Print the total with a 5% sales tax.<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$my_cart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTotal</span><span style="color: #007700">(</span><span style="color: #0000BB">0.05</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// The result is 54.29<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <div class="example" id="example-5">
    <p><strong>Приклад #5 Automatic binding of <code class="literal">$this</code></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">Test<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">testing</span><span style="color: #007700">()<br />    {<br />        return function() {<br />            </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">);<br />        };<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$object </span><span style="color: #007700">= new </span><span style="color: #0000BB">Test</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$function </span><span style="color: #007700">= </span><span style="color: #0000BB">$object</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">testing</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$function</span><span style="color: #007700">();<br />    <br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(Test)#1 (0) {
}
</pre></div>
    </div>
   </div>

   <p class="para">
    When declared in the context of a class, the current class is
    automatically bound to it, making <code class="literal">$this</code> available
    inside of the function&#039;s scope. If this automatic binding of the current
    class is not wanted, then
    <a href="functions.anonymous.php#functions.anonymous-functions.static" class="link">static anonymous
    functions</a> may be used instead.
   </p>

   <div class="sect2" id="functions.anonymous-functions.static">
    <h3 class="title">Static anonymous functions</h3>
    <p class="para">
     Anonymous functions may be declared statically. This
     prevents them from having the current class automatically bound to
     them. Objects may also not be bound to them at runtime.
    </p>
    <p class="para">
     <div class="example" id="example-6">
      <p><strong>Приклад #6 Attempting to use <code class="literal">$this</code> inside a static anonymous function</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">Foo<br /></span><span style="color: #007700">{<br />    function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #0000BB">$func </span><span style="color: #007700">= static function() {<br />            </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">);<br />        };<br />        </span><span style="color: #0000BB">$func</span><span style="color: #007700">();<br />    }<br />};<br />new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

      <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
Notice: Undefined variable: this in %s on line %d
NULL
</pre></div>
      </div>
     </div>
    </p>

    <p class="para">
     <div class="example" id="example-7">
      <p><strong>Приклад #7 Attempting to bind an object to a static anonymous function</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$func </span><span style="color: #007700">= static function() {<br />    </span><span style="color: #FF8000">// function body<br /></span><span style="color: #007700">};<br /></span><span style="color: #0000BB">$func </span><span style="color: #007700">= </span><span style="color: #0000BB">$func</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindTo</span><span style="color: #007700">(new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$func</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

      <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
Warning: Cannot bind an instance to a static closure in %s on line %d
</pre></div>
      </div>
     </div>
    </p>
   </div>
   
   <div class="sect2">
    <h3 class="title">Журнал змін</h3>
    <p class="para">
     <table class="doctable informaltable">
      
       <thead>
        <tr>
         <th>Версія</th>
         <th>Опис</th>
        </tr>

       </thead>

       <tbody class="tbody">
        <tr>
         <td>8.3.0</td>
         <td>
          Closures created from <a href="language.oop5.magic.php" class="link">magic
          methods</a> can accept named parameters.
         </td>
        </tr>

        <tr>
         <td>7.1.0</td>
         <td>
          Anonymous functions may not close over <a href="language.variables.predefined.php" class="link">superglobals</a>,
          <var class="varname">$this</var>, or any variable with the same name as a
          parameter.
         </td>
        </tr>

       </tbody>
      
     </table>

    </p>
   </div>

   <div class="sect2">
    <h3 class="title">Примітки</h3>
    <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
     <span class="simpara">
      It is possible to use <span class="function"><a href="function.func-num-args.php" class="function">func_num_args()</a></span>,
      <span class="function"><a href="function.func-get-arg.php" class="function">func_get_arg()</a></span>, and <span class="function"><a href="function.func-get-args.php" class="function">func_get_args()</a></span>
      from within a closure.
     </span>
    </p></blockquote>
   </div>

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