Category
language
Problem
PHP 8.3+ allows typed class constants (public const array X = [1, 2];, const string NAME = 'x';). Zend validates types at compile time and stores values in the class constant table. This compiler accepts the syntax but defineClass() rejects non-scalar constant values even when the RHS is a literal array:
LogicException: Class constant value must be a compile-time constant
(VM stack: lib/VM.php:2415, May 2026).
Distinct from #3196 (object constants const X = new Foo();).
php-src reference
Repro (today)
<?php
class C {
public const array X = [1, 2];
}
echo C::X[0];
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php /path/to/repro.php'
Expected (Zend): 1.
Actual: fatal Class constant value must be a compile-time constant during class registration.
Scope (this repo)
lib/Compiler.php — materialize literal array/string/int/float/bool/null const values at compile time
lib/VM.php — store typed constants; enforce type on fetch; C::X[$i] dim fetch on array constants
- Type mismatch assignment at declare time → compile-time
TypeError (Zend parity)
test/compliance/cases/language/typed_class_const.phpt
script/capability-syntax.php — document PHP 8.3 typed constants row
Done when
Category
languageProblem
PHP 8.3+ allows typed class constants (
public const array X = [1, 2];,const string NAME = 'x';). Zend validates types at compile time and stores values in the class constant table. This compiler accepts the syntax butdefineClass()rejects non-scalar constant values even when the RHS is a literal array:(VM stack:
lib/VM.php:2415, May 2026).Distinct from #3196 (object constants
const X = new Foo();).php-src reference
Zend/zend_compile.c— typed const validation (zend_compile_class_const)Zend/zend_constants.c— constant table + type infoZend/zend_type.c—zend_verify_const_typeRepro (today)
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php /path/to/repro.php'Expected (Zend):
1.Actual: fatal
Class constant value must be a compile-time constantduring class registration.Scope (this repo)
lib/Compiler.php— materialize literal array/string/int/float/bool/null const values at compile timelib/VM.php— store typed constants; enforce type on fetch;C::X[$i]dim fetch on array constantsTypeError(Zend parity)test/compliance/cases/language/typed_class_const.phptscript/capability-syntax.php— document PHP 8.3 typed constants rowDone when
public const array X = [1,2];andconst string S = 'a';work on VM; wrong-type value fails at compile timeecho C::X[0];prints1in repro