On 15/04/2025 11:49, Jorg Sowa wrote:
I gave this example only to point out that this case should be
mentioned in the RFC to dispel doubts what's intended and eventually
put such test case into the PR.
Sure, but there was a mistake in your example - Closure::current(10)
should be Closure::current()(10), i.e. "get the current closure, and
then immediately invoke it, passing 10".
More readable if you put it in a local variable:
$a = function (int $numberA) {
$b = function (int $numberB) {
$selfReference = Closure::getCurrent();
if($numberB < 10) {
return $selfReference(10);
}
return $numberB;
};
return $b($numberA) + $numberA;
};
var_dump($a(4));
The local var $selfReference refers to the same Closure object as the
outer var $b, because that's the closure that's "current" on the line
where Closure::getCurrent() is called.
--
Rowan Tommins
[IMSoP]