Skip to content

Language: User-defined constants (const, define()) and class constants #204

Description

@PurHur

Problem

Web apps use const VERSION = '1.0';, define('DEBUG', true);, and class constants for config. The compiler lowers classes and globals but user-defined constants are incomplete:

examples/003-MiniWebApp/config.php is a plain return [...] array today; constants become important once config moves to define() / class constants.

Goal

const APP_NAME = 'MiniWeb';
define('MAX_UPLOAD', 1024);
class Config { public const ENV = 'prod'; }
echo APP_NAME, Config::ENV;

VM + JIT + AOT: immutable fetches; define() registers before use in same request.

Implementation hints

Compiler (lib/Compiler.php)

Construct Lowering
Stmt\Const_ Add to compilation unit constant table at compile time
define() Builtin or opcode TYPE_DEFINE with name + value
Class const Extend compileClassBody — store in class metadata, TYPE_CLASS_CONST_FETCH

VM (lib/VM.php)

  • Global constant table keyed by name (case-sensitive)
  • Class constant table on class entry
  • define() checks defined() / duplicate semantics (Zend subset)

JIT / AOT

  • Inline literal constants where opcode is TYPE_CONST_FETCH with known value
  • JIT: emit global as LLVM global or immediate

Lint

  • Register unsupported const forms in UnsupportedRegistry until implemented

Tests

  • test/compliance/cases/const_global.phpt
  • test/compliance/cases/define_runtime.phpt
  • test/compliance/cases/class_const.phpt
  • script/capability-matrix.php if builtins added

Acceptance criteria

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  php bin/vm.php -r 'const X=42; define("Y",1); echo X+Y;'

Prints 43. JIT parity when #98 LLVM group runs.

Verification (local / Docker only)

./script/ci-local.sh --filter const_

No GitHub Actions.

Dependencies

Links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions