Skip to content

REPL cannot declare constants with const #11

Description

@MarcelloDuarte

Top-level const declarations are not implemented:

phunkie > const GREETING = "hello"
Error: Evaluation error: Unsupported statement type: PhpParser\Node\Stmt\Const_
phunkie > GREETING
Error: Evaluation error: Undefined constant: GREETING

evaluateAst() has no Node\Stmt\Const_ branch, so it falls through to the catch-all at src/Functions/evaluation.php:286. define() and class constants both work; only the const keyword at statement level is missing.

This blocks acceptance coverage for three PHP 8.5 features, all of which are about what may now appear in a constant expression:

  • closures in constant expressions - const CB = static function () { return 42; };
  • first-class callables in constant expressions - const SL = strlen(...);
  • casts in constant expressions - const T = (int) 0.3;

and one more that needs the same statement type:

  • attributes on global constants, including #[\Deprecated] on a constant

Worth deciding how a redeclared constant should behave, since re-entering a line is normal in a REPL and PHP 8.5 deprecates constant redeclaration.

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