Re: Closure::getCurrent() for recursion

From: Date: Tue, 01 Apr 2025 00:18:05 +0000
Subject: Re: Closure::getCurrent() for recursion
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message

On 2025-04-01 09:38, Ilija Tovilo wrote:
Hi everyone This is essentially already possible through a by-reference capture. $fibonacci = function (int $n) use (&$fibonacci) { ... }; This cannot be a by-value capture because by the time the closure is created and variables are bound, $fibonacci is not assigned yet. The downside of by-reference capturing is that if $fibonacci is accidentally reassigned, the reference within the closure is also changed. However, I consider a language change largely unnecessary. Instead, this may be solved with a very simple static function: Closure::getCurrent().
How about mutual recursion, where two of these functions call each other? Currently one can write $left = function(array $a) use(&$second) { ... }; $second = function(array $a) use($first) { ... }; (The second can capture the first by value, but the first still needs to capture the second by reference.) With Closure::getCurrent(), how does either function reference the other?

Thread (15 messages)

« previous php.internals (#126991) next »