<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'closure.getcurrent.php',
    1 => 'Closure::getCurrent',
    2 => 'Returns the currently executing closure',
  ),
  'up' => 
  array (
    0 => 'class.closure.php',
    1 => 'Closure',
  ),
  'prev' => 
  array (
    0 => 'closure.fromcallable.php',
    1 => 'Closure::fromCallable',
  ),
  'next' => 
  array (
    0 => 'class.stdclass.php',
    1 => 'stdClass',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/predefined/closure/getcurrent.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="closure.getcurrent" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Closure::getCurrent</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.5.0)</p><p class="refpurpose"><span class="refname">Closure::getCurrent</span> &mdash; <span class="dc-title">Returns the currently executing closure</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-closure.getcurrent-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="methodname"><strong>Closure::getCurrent</strong></span>(): <span class="type"><a href="class.closure.php" class="type Closure">Closure</a></span></div>

  <p class="para rdfs-comment">
   Returns the currently executing closure. This method is primarily useful
   for implementing recursive closures without needing to capture a reference
   to the closure variable using the <code class="literal">use</code> keyword.
  </p>
  <p class="para">
   This method must be called from within a closure; calling it outside of a
   closure context will result in <code class="literal">Error: Current function is not a closure.</code>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-closure.getcurrent-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">Diese Funktion besitzt keine Parameter.</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-closure.getcurrent-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Returns the currently executing <span class="classname"><a href="class.closure.php" class="classname">Closure</a></span> instance.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-closure.getcurrent-errors">
  <h3 class="title">Fehler/Exceptions</h3>
  <p class="para">
   Throws an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> if called outside of a closure
   context.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-closure.getcurrent-examples">
  <h3 class="title">Beispiele</h3>
  <div class="example" id="closure.getcurrent.example.basic">
   <p><strong>Beispiel #1 <span class="methodname"><strong>Closure::getCurrent()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    Using <span class="methodname"><strong>Closure::getCurrent()</strong></span> to implement a
    recursive Fibonacci function:
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fibonacci </span><span style="color: #007700">= function (</span><span style="color: #0000BB">int $n</span><span style="color: #007700">) {<br />    if (</span><span style="color: #0000BB">$n </span><span style="color: #007700">=== </span><span style="color: #0000BB">0 </span><span style="color: #007700">|| </span><span style="color: #0000BB">$n </span><span style="color: #007700">=== </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br />        return </span><span style="color: #0000BB">$n</span><span style="color: #007700">;<br />    }<br /><br />    </span><span style="color: #0000BB">$fn </span><span style="color: #007700">= </span><span style="color: #0000BB">Closure</span><span style="color: #007700">::</span><span style="color: #0000BB">getCurrent</span><span style="color: #007700">();<br />    return </span><span style="color: #0000BB">$fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$n </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">) + </span><span style="color: #0000BB">$fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$n </span><span style="color: #007700">- </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />};<br /><br />echo </span><span style="color: #0000BB">$fibonacci</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">); </span><span style="color: #FF8000">// Outputs: 55<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <div class="example" id="closure.getcurrent.example.comparison">
   <p><strong>Beispiel #2 Comparison with traditional approach</strong></p>
   <div class="example-contents"><p>
    Prior to PHP 8.5, implementing recursive closures required capturing a reference
    to the closure variable using the <code class="literal">use</code> keyword:
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Traditional approach (still works in PHP 8.5)<br /></span><span style="color: #0000BB">$fibonacci </span><span style="color: #007700">= function (</span><span style="color: #0000BB">int $n</span><span style="color: #007700">) use (&amp;</span><span style="color: #0000BB">$fibonacci</span><span style="color: #007700">) {<br />    if (</span><span style="color: #0000BB">$n </span><span style="color: #007700">=== </span><span style="color: #0000BB">0</span><span style="color: #007700">) return </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />    if (</span><span style="color: #0000BB">$n </span><span style="color: #007700">=== </span><span style="color: #0000BB">1</span><span style="color: #007700">) return </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />    return </span><span style="color: #0000BB">$fibonacci</span><span style="color: #007700">(</span><span style="color: #0000BB">$n </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">) + </span><span style="color: #0000BB">$fibonacci</span><span style="color: #007700">(</span><span style="color: #0000BB">$n </span><span style="color: #007700">- </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />};<br /><br />echo </span><span style="color: #0000BB">$fibonacci</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">); </span><span style="color: #FF8000">// Outputs: 55<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>
    The <span class="methodname"><strong>Closure::getCurrent()</strong></span> approach eliminates the need to
    declare the variable with a reference in the <code class="literal">use</code> clause,
    making the code cleaner and less error-prone.
   </p></div>
  </div>
 </div>


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