Category
language
Problem
PHP 8.3 adds typed class constants. Typed constants inside traits must behave like Zend when imported into a class:
- The constant must carry its declared type.
- The value must be validated against the declared type at compile time (or at least fail with the same fatal/error behavior as Zend).
- If a class overrides a trait constant, type compatibility rules must match Zend.
This repo already tracks typed interface constants (#5980) and interface constant inheritance compatibility (#5982). Traits are a distinct path (trait import/flattening) and need separate parity coverage.
php-src reference
Zend/zend_compile.c — typed constant declaration and validation
Zend/zend_inheritance.c — trait constant import and conflict resolution
Repro
Save as test/repro-maintainer/typed_trait_constants.php:
<?php
trait TGood {
public const int X = 1;
}
final class C {
use TGood;
}
echo C::X, "\n";
Also validate error parity (value/type mismatch) with test/repro-maintainer/typed_trait_constants_mismatch.php:
<?php
trait TBad {
public const int X = '1';
}
final class C {
use TBad;
}
Run:
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro-maintainer/typed_trait_constants.php
php bin/vm.php test/repro-maintainer/typed_trait_constants.php
php test/repro-maintainer/typed_trait_constants_mismatch.php
php bin/vm.php test/repro-maintainer/typed_trait_constants_mismatch.php
'
Expected (Zend):
- First script prints
1.
- Second script fails with a compile-time fatal/error indicating the constant value is not of type
int.
Observed (this compiler today):
- Either parse/compile failure for typed constants in traits, or missing type enforcement, or incorrect constant metadata.
Scope (this repo)
lib/Compiler.php — lowering typed const declarations inside traits
lib/VM/ — trait constant import into class entry, preserving declared type metadata
lib/VM/Constant* / metadata structures — ensure typed const type is carried through
- Tests:
test/compliance/cases/language/typed_trait_constants.phpt
test/compliance/cases/language/typed_trait_constants_mismatch.phpt
Done when
Labels
php-src-strict (default) — error shapes and type enforcement must match Zend.
Links
Category
languageProblem
PHP 8.3 adds typed class constants. Typed constants inside traits must behave like Zend when imported into a class:
This repo already tracks typed interface constants (#5980) and interface constant inheritance compatibility (#5982). Traits are a distinct path (trait import/flattening) and need separate parity coverage.
php-src reference
Zend/zend_compile.c— typed constant declaration and validationZend/zend_inheritance.c— trait constant import and conflict resolutionRepro
Save as
test/repro-maintainer/typed_trait_constants.php:Also validate error parity (value/type mismatch) with
test/repro-maintainer/typed_trait_constants_mismatch.php:Run:
Expected (Zend):
1.int.Observed (this compiler today):
Scope (this repo)
lib/Compiler.php— lowering typed const declarations inside traitslib/VM/— trait constant import into class entry, preserving declared type metadatalib/VM/Constant*/ metadata structures — ensure typed const type is carried throughtest/compliance/cases/language/typed_trait_constants.phpttest/compliance/cases/language/typed_trait_constants_mismatch.phptDone when
C::Xon VM./script/ci-fast.sh --filter typed_trait_constantsis greenLabels
php-src-strict(default) — error shapes and type enforcement must match Zend.Links