Skip to content

Closure::getCurrent() does not work in the REPL #14

Description

@MarcelloDuarte

PHP 8.5 added Closure::getCurrent(), which returns the closure currently executing so it can recurse without capturing itself. It fails in the REPL:

phunkie > $f = function () { return Closure::getCurrent() instanceof Closure; }
$f: Callable = <function>
phunkie > $f()
Error: Evaluation error: Function call failed: Closure evaluation failed: Static call failed: Current function is not a closure

The same code works under php -r.

The cause is structural rather than a missing branch: the REPL interprets closure bodies itself (evaluateClosure / evaluateArrowFunction walk the AST) instead of compiling a real PHP closure, so at the point Closure::getCurrent() runs there is no enclosing PHP closure on the stack for it to return.

Filed for visibility rather than as an easy fix - honouring it means either compiling user closures into real closures, or special-casing Closure::getCurrent() to return the interpreted closure the evaluator is currently inside. The second is narrower and probably the right first step.

Lower priority than the other gaps, since getCurrent() is a niche convenience, but it is the one PHP 8.5 feature that the REPL's interpretation strategy actively prevents rather than merely not having got to yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions