isset()-Materialized Magic Property Skips __get()
?? and empty() on an inaccessible property call __isset() first. If __isset() materializes the property, by writing directly into the object’s property table, for example $this->$name = …, PHP 8.6 returns that freshly-written value directly instead of calling __get() afterwards. Until PHP 8.6, __get() was still called even though the property now exists, and its return value was used instead of the value __isset() had just written. Plain isset() is unaffected by this change.
PHP code
<?php
#[AllowDynamicProperties]
class Magic {
public function __isset($name) {
echo "__isset($name) called\n";
$this->$name = 'materialized-by-isset';
return true;
}
public function __get($name) {
echo "__get($name) called\n";
return 'from __get';
}
}
$m = new Magic();
var_dump($m->x ?? 'default');
?>
Before
__isset(x) called
__get(x) called
string(10) "from __get"
After
__isset(x) called
string(21) "materialized-by-isset"
PHP version change
This behavior changed in 8.6.