mirror of
https://github.com/php/doc-en.git
synced 2026-03-23 23:32:18 +01:00
120 lines
3.3 KiB
XML
120 lines
3.3 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<refentry xml:id="closure.getcurrent" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<refnamediv>
|
|
<refname>Closure::getCurrent</refname>
|
|
<refpurpose>Returns the currently executing closure</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis role="Closure">
|
|
<modifier>public</modifier> <modifier>static</modifier> <type>Closure</type><methodname>Closure::getCurrent</methodname>
|
|
<void/>
|
|
</methodsynopsis>
|
|
<para>
|
|
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 <literal>use</literal> keyword.
|
|
</para>
|
|
<para>
|
|
This method must be called from within a closure; calling it outside of a
|
|
closure context will result in <literal>Error: Current function is not a closure.</literal>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
&no.function.parameters;
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Returns the currently executing <classname>Closure</classname> instance.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="errors">
|
|
&reftitle.errors;
|
|
<para>
|
|
Throws an <classname>Error</classname> if called outside of a closure
|
|
context.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<example xml:id="closure.getcurrent.example.basic">
|
|
<title><methodname>Closure::getCurrent</methodname> example</title>
|
|
<para>
|
|
Using <methodname>Closure::getCurrent</methodname> to implement a
|
|
recursive Fibonacci function:
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$fibonacci = function (int $n) {
|
|
if ($n === 0 || $n === 1) {
|
|
return $n;
|
|
}
|
|
|
|
$fn = Closure::getCurrent();
|
|
return $fn($n - 1) + $fn($n - 2);
|
|
};
|
|
|
|
echo $fibonacci(10); // Outputs: 55
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
<example xml:id="closure.getcurrent.example.comparison">
|
|
<title>Comparison with traditional approach</title>
|
|
<para>
|
|
Prior to PHP 8.5, implementing recursive closures required capturing a reference
|
|
to the closure variable using the <literal>use</literal> keyword:
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// Traditional approach (still works in PHP 8.5)
|
|
$fibonacci = function (int $n) use (&$fibonacci) {
|
|
if ($n === 0) return 0;
|
|
if ($n === 1) return 1;
|
|
return $fibonacci($n - 1) + $fibonacci($n - 2);
|
|
};
|
|
|
|
echo $fibonacci(10); // Outputs: 55
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
The <methodname>Closure::getCurrent</methodname> approach eliminates the need to
|
|
declare the variable with a reference in the <literal>use</literal> clause,
|
|
making the code cleaner and less error-prone.
|
|
</para>
|
|
</example>
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|