Category
language · php-src-strict · PHP 8.4 property hooks
Problem
PHP 8.4 allows readonly properties with set hooks initialized from the constructor (hook writes during object construction):
class C {
public readonly int $x {
get => $this->x;
set { $this->x = $value; }
}
public function __construct() {
$this->x = 0;
}
}
$c = new C();
$c->x = 1; // runtime Error after init
This compiler rejects at compile:
Hooked properties cannot be readonly
Maintainer repro: test/repro/maintainer_gap_readonly_hook_write_exception.php.
php-src reference
Repro
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php test/repro/maintainer_gap_readonly_hook_write_exception.php
'
| Engine |
Compile |
Runtime write after construct |
| Zend PHP 8.4+ |
OK |
catchable Error (not compile fail) |
| VM today |
compile fail |
n/a |
Scope
lib/Compiler.php — allow readonly + hooks; enforce post-construct write rules at runtime
lib/VM.php — set hook + readonly interaction
test/compliance/cases/language/readonly_property_hook.phpt
Done when
Related
Category
language· php-src-strict · PHP 8.4 property hooksProblem
PHP 8.4 allows readonly properties with set hooks initialized from the constructor (hook writes during object construction):
This compiler rejects at compile:
Maintainer repro:
test/repro/maintainer_gap_readonly_hook_write_exception.php.php-src reference
Zend/zend_compile.c— readonly + hook validationZend/zend_property_hooks.c— set hook during construction vs post-init write ErrorRepro
Scope
lib/Compiler.php— allow readonly + hooks; enforce post-construct write rules at runtimelib/VM.php— set hook + readonly interactiontest/compliance/cases/language/readonly_property_hook.phptDone when
$c->x = 1throws catchable Error matching Zend message class./script/ci-fast.sh --filter readonly_property_hookgreenRelated