Closure::getCurrentReturns the currently executing closure Description
public static Closure Closure::getCurrent()
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
This method must be called from within a closure; calling it outside of a
closure context will result in ParametersThis function has no parameters. Return ValuesReturns the currently executing Closure instance. Errors/ExceptionsThrows an Error if called outside of a closure context. ExamplesExample #1 Closure::getCurrent example Using Closure::getCurrent to implement a recursive Fibonacci function: Example #2 Comparison with traditional approach
Prior to PHP 8.5, implementing recursive closures required capturing a reference
to the closure variable using the
The Closure::getCurrent approach eliminates the need to
declare the variable with a reference in the |