Skip to content

Commit 1ea7f94

Browse files
committed
sync with en rev
1 parent c89f4a5 commit 1ea7f94

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

language/predefined/closure.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!-- EN-Revision: da5ebdf7e0b710ac4f20b53f7c09cf9fa1346d3e Maintainer: ae Status: ready --><!-- CREDITS: fabioluciano,lhsazevedo,ae,leonardolara -->
2+
<!-- EN-Revision: d74f3573b3831b2db8f31f774cff32eb8560ba6e Maintainer: ae Status: ready --><!-- CREDITS: fabioluciano,lhsazevedo,ae,leonardolara -->
33
<reference xml:id="class.closure" role="class" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
44

55
<title>A classe Closure</title>
@@ -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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- EN-Revision: 6fc2c24b6b465f4057d076a39c6910b8a484daad Maintainer: leonardolara Status: ready -->
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>Retorna o closure em execução no momento</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+
Retorna o closure em execução no momento. Este método é útil principalmente
17+
para implementar closures recursivos sem a necessidade de capturar uma referência
18+
à variável do closure com a palavra-chave <literal>use</literal>.
19+
</para>
20+
<para>
21+
Este método precisa ser chamado de dentro de um closure; chamá-lo de fora de um
22+
contexto de closure resultará em um erro.
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+
Retorna a instância do <classname>Closure</classname> em execução.
35+
</para>
36+
</refsect1>
37+
38+
<refsect1 role="errors">
39+
&reftitle.errors;
40+
<para>
41+
Lança um <classname>Error</classname> se chamado de fora de um contexto de
42+
closure.
43+
</para>
44+
</refsect1>
45+
46+
<refsect1 role="examples">
47+
&reftitle.examples;
48+
<example xml:id="closure.getcurrent.example.basic">
49+
<title>Exemplo de <methodname>Closure::getCurrent</methodname></title>
50+
<para>
51+
Usando <methodname>Closure::getCurrent</methodname> para implementar uma
52+
função de Fibonacci recursiva:
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); // Exibe: 55
67+
?>
68+
]]>
69+
</programlisting>
70+
</example>
71+
<example xml:id="closure.getcurrent.example.comparison">
72+
<title>Comparação com método tradicional</title>
73+
<para>
74+
Antes do PHP 8.5, para implementar closures recursivos era necessário capturar uma referência
75+
à variável do closure usando a palavra-chave <literal>use</literal>:
76+
</para>
77+
<programlisting role="php">
78+
<![CDATA[
79+
<?php
80+
// Método tradicional (ainda funciona no 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); // Exibe: 55
88+
?>
89+
]]>
90+
</programlisting>
91+
<para>
92+
O método <methodname>Closure::getCurrent</methodname> elimita a necessidade de
93+
declarar a variável com uma referência na cláusula <literal>use</literal>,
94+
tornando o código mais limpo e menos suscetível a erros.
95+
</para>
96+
</example>
97+
</refsect1>
98+
99+
</refentry>
100+
<!-- Keep this comment at the end of the file
101+
Local variables:
102+
mode: sgml
103+
sgml-omittag:t
104+
sgml-shorttag:t
105+
sgml-minimize-attributes:nil
106+
sgml-always-quote-attributes:t
107+
sgml-indent-step:1
108+
sgml-indent-data:t
109+
indent-tabs-mode:nil
110+
sgml-parent-document:nil
111+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
112+
sgml-exposed-tags:nil
113+
sgml-local-catalogs:nil
114+
sgml-local-ecat-files:nil
115+
End:
116+
vim600: syn=xml fen fdm=syntax fdl=2 si
117+
vim: et tw=78 syn=sgml
118+
vi: ts=1 sw=1
119+
-->

0 commit comments

Comments
 (0)