Category
language · php-src-strict
Problem
The null-coalescing operator ?? on a hooked property must check whether the property backing is initialized without calling the get hook (same rule as isset()). This compiler invokes the get hook before applying ??, so throwing getters emit a catchable/uncaught exception even when the backing value is present.
php-src reference
Repro
Save as test/repro/issue_property_hook_coalesce.php:
<?php
class C {
public string $x {
get { throw new Exception('get must not run for ??'); }
set => $this->backing = $value;
}
private string $backing = 'a';
}
$c = new C();
var_dump($c->x ?? 'default');
echo "ok\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/issue_property_hook_coalesce.php
php bin/vm.php test/repro/issue_property_hook_coalesce.php
'
| Runtime |
Output |
| Zend PHP 8.4+ |
string(1) "a" then ok |
| VM (wrong) |
Exception from get hook (may still print default after unwinding) |
Also cover uninitialized backing: var_dump($c->x ?? 'default'); after unset($c->x) when unset is allowed — must return default without get hook.
Scope (PHP-in-PHP)
| Layer |
Path |
| VM |
lib/VM.php — coalesce opcode on hooked properties; share probe with ??= (#6472) |
| Compiler |
Lowering unchanged |
| JIT/AOT |
Optional follow-up when hook coalesce appears in hot paths |
| Tests |
test/compliance/cases/language/property_hook_coalesce.phpt |
Done when (php-src-strict)
Related
#8901 empty/isset · #6472 ??= backing check · #8870 foreach by-ref · #1492
Category
language· php-src-strictProblem
The null-coalescing operator
??on a hooked property must check whether the property backing is initialized without calling the get hook (same rule asisset()). This compiler invokes the get hook before applying??, so throwing getters emit a catchable/uncaught exception even when the backing value is present.php-src reference
Zend/zend_property_hooks.c—zend_isset_property_ex, coalesce fast pathZend/zend_compile.c—ZEND_AST_COALESCEon hooked propertiesRepro
Save as
test/repro/issue_property_hook_coalesce.php:string(1) "a"thenokdefaultafter unwinding)Also cover uninitialized backing:
var_dump($c->x ?? 'default');afterunset($c->x)when unset is allowed — must returndefaultwithout get hook.Scope (PHP-in-PHP)
lib/VM.php— coalesce opcode on hooked properties; share probe with??=(#6472)test/compliance/cases/language/property_hook_coalesce.phptDone when (php-src-strict)
string(1) "a"+okon VM with no get-hook invocation??returns default without get hook./script/ci-fast.sh --filter property_hook_coalescegreenRelated
#8901 empty/isset · #6472
??=backing check · #8870 foreach by-ref · #1492