[DOC-CVS] [doc-en] master: document getcurrent

[email protected] (Karoly Negyesi)
Newsgroups php.doc.cvs
Message-ID <[email protected]>
Author: Karoly Negyesi (chx)
Date: 2026-02-02T09:34:00+01:00

Commit: https://github.com/php/doc-en/commit/d74f3573b3831b2db8f31f774cff32eb8560ba6e
Raw diff: https://github.com/php/doc-en/commit/d74f3573b3831b2db8f31f774cff32eb8560ba6e.diff

document getcurrent

Changed paths:
  A  language/predefined/closure/getcurrent.xml
  M  language/predefined/closure.xml
  M  language/predefined/versions.xml


Diff:

diff --git a/language/predefined/closure.xml b/language/predefined/closure.xml
index c9472c5a3929..246019789b89 100644
--- a/language/predefined/closure.xml
+++ b/language/predefined/closure.xml
@@ -83,6 +83,7 @@
  &language.predefined.closure.bindto;
  &language.predefined.closure.call;
  &language.predefined.closure.fromcallable;
+ &language.predefined.closure.getcurrent;
 
 </reference>
 <!-- Keep this comment at the end of the file
diff --git a/language/predefined/closure/getcurrent.xml b/language/predefined/closure/getcurrent.xml
new file mode 100644
index 000000000000..1b0bad5a9a80
--- /dev/null
+++ b/language/predefined/closure/getcurrent.xml
@@ -0,0 +1,129 @@
+<?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 an error.
+  </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 (0 === $n || 1 === $n) {
+        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>
+
+ <refsect1 role="seealso">
+  &reftitle.seealso;
+  <simplelist>
+   <member><link linkend="functions.anonymous">Anonymous functions</link></member>
+   <member><methodname>Closure::bind</methodname></member>
+   <member><methodname>Closure::bindTo</methodname></member>
+   <member><methodname>Closure::call</methodname></member>
+  </simplelist>
+ </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
+-->
diff --git a/language/predefined/versions.xml b/language/predefined/versions.xml
index e76bf2bc175f..70f57a4404d7 100644
--- a/language/predefined/versions.xml
+++ b/language/predefined/versions.xml
@@ -83,6 +83,7 @@
  <function name="closure::bindto" from="PHP 5 &gt;= 5.4.0, PHP 7, PHP 8"/>
  <function name="closure::call" from="PHP 7, PHP 8"/>
  <function name="closure::fromcallable" from="PHP 7 &gt;= 7.1.0"/>
+ <function name="closure::getcurrent" from="PHP 8 &gt;= 8.5.0"/>
 
  <function name="namespaces" from="PHP 5 &gt;= 5.3.0, PHP 7, PHP 8"/>
  <function name="generators" from="PHP 5 &gt;= 5.5.0, PHP 7, PHP 8"/>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.