Skip to content

Commit d74f357

Browse files
committed
document getcurrent
1 parent d9cfd78 commit d74f357

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

language/predefined/closure.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
&language.predefined.closure.bindto;
8484
&language.predefined.closure.call;
8585
&language.predefined.closure.fromcallable;
86+
&language.predefined.closure.getcurrent;
8687

8788
</reference>
8889
<!-- Keep this comment at the end of the file
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- $Revision$ -->
3+
<refentry xml:id="closure.getcurrent" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
4+
<refnamediv>
5+
<refname>Closure::getCurrent</refname>
6+
<refpurpose>Returns the currently executing closure</refpurpose>
7+
</refnamediv>
8+
9+
<refsect1 role="description">
10+
&reftitle.description;
11+
<methodsynopsis role="Closure">
12+
<modifier>public</modifier> <modifier>static</modifier> <type>Closure</type><methodname>Closure::getCurrent</methodname>
13+
<void/>
14+
</methodsynopsis>
15+
<para>
16+
Returns the currently executing closure. This method is primarily useful
17+
for implementing recursive closures without needing to capture a reference
18+
to the closure variable using the <literal>use</literal> keyword.
19+
</para>
20+
<para>
21+
This method must be called from within a closure; calling it outside of a
22+
closure context will result in an error.
23+
</para>
24+
</refsect1>
25+
26+
<refsect1 role="parameters">
27+
&reftitle.parameters;
28+
&no.function.parameters;
29+
</refsect1>
30+
31+
<refsect1 role="returnvalues">
32+
&reftitle.returnvalues;
33+
<para>
34+
Returns the currently executing <classname>Closure</classname> instance.
35+
</para>
36+
</refsect1>
37+
38+
<refsect1 role="errors">
39+
&reftitle.errors;
40+
<para>
41+
Throws an <classname>Error</classname> if called outside of a closure
42+
context.
43+
</para>
44+
</refsect1>
45+
46+
<refsect1 role="examples">
47+
&reftitle.examples;
48+
<example xml:id="closure.getcurrent.example.basic">
49+
<title><methodname>Closure::getCurrent</methodname> example</title>
50+
<para>
51+
Using <methodname>Closure::getCurrent</methodname> to implement a
52+
recursive Fibonacci function:
53+
</para>
54+
<programlisting role="php">
55+
<![CDATA[
56+
<?php
57+
$fibonacci = function (int $n) {
58+
if (0 === $n || 1 === $n) {
59+
return $n;
60+
}
61+
62+
$fn = Closure::getCurrent();
63+
return $fn($n - 1) + $fn($n - 2);
64+
};
65+
66+
echo $fibonacci(10); // Outputs: 55
67+
?>
68+
]]>
69+
</programlisting>
70+
</example>
71+
<example xml:id="closure.getcurrent.example.comparison">
72+
<title>Comparison with traditional approach</title>
73+
<para>
74+
Prior to PHP 8.5, implementing recursive closures required capturing a reference
75+
to the closure variable using the <literal>use</literal> keyword:
76+
</para>
77+
<programlisting role="php">
78+
<![CDATA[
79+
<?php
80+
// Traditional approach (still works in PHP 8.5)
81+
$fibonacci = function (int $n) use (&$fibonacci) {
82+
if ($n === 0) return 0;
83+
if ($n === 1) return 1;
84+
return $fibonacci($n - 1) + $fibonacci($n - 2);
85+
};
86+
87+
echo $fibonacci(10); // Outputs: 55
88+
?>
89+
]]>
90+
</programlisting>
91+
<para>
92+
The <methodname>Closure::getCurrent</methodname> approach eliminates the need to
93+
declare the variable with a reference in the <literal>use</literal> clause,
94+
making the code cleaner and less error-prone.
95+
</para>
96+
</example>
97+
</refsect1>
98+
99+
<refsect1 role="seealso">
100+
&reftitle.seealso;
101+
<simplelist>
102+
<member><link linkend="functions.anonymous">Anonymous functions</link></member>
103+
<member><methodname>Closure::bind</methodname></member>
104+
<member><methodname>Closure::bindTo</methodname></member>
105+
<member><methodname>Closure::call</methodname></member>
106+
</simplelist>
107+
</refsect1>
108+
109+
</refentry>
110+
<!-- Keep this comment at the end of the file
111+
Local variables:
112+
mode: sgml
113+
sgml-omittag:t
114+
sgml-shorttag:t
115+
sgml-minimize-attributes:nil
116+
sgml-always-quote-attributes:t
117+
sgml-indent-step:1
118+
sgml-indent-data:t
119+
indent-tabs-mode:nil
120+
sgml-parent-document:nil
121+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
122+
sgml-exposed-tags:nil
123+
sgml-local-catalogs:nil
124+
sgml-local-ecat-files:nil
125+
End:
126+
vim600: syn=xml fen fdm=syntax fdl=2 si
127+
vim: et tw=78 syn=sgml
128+
vi: ts=1 sw=1
129+
-->

language/predefined/versions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<function name="closure::bindto" from="PHP 5 &gt;= 5.4.0, PHP 7, PHP 8"/>
8484
<function name="closure::call" from="PHP 7, PHP 8"/>
8585
<function name="closure::fromcallable" from="PHP 7 &gt;= 7.1.0"/>
86+
<function name="closure::getcurrent" from="PHP 8 &gt;= 8.5.0"/>
8687

8788
<function name="namespaces" from="PHP 5 &gt;= 5.3.0, PHP 7, PHP 8"/>
8889
<function name="generators" from="PHP 5 &gt;= 5.5.0, PHP 7, PHP 8"/>

0 commit comments

Comments
 (0)