<?php
class Coalescing
{
protected $attributes = [
'attribute1' => 'value1',
];
public function __isset($name)
{
return array_key_exists($name, $this->attributes);
}
public function __get($name)
{
print "GET\n";
if (array_key_exists($name, $this->attributes)) {
return $this->attributes[$name];
}
trigger_error("Property $name does not exist");
return null;
}
}
$coal = new Coalescing();
var_dump($coal->attribute1 ?? 'default');
//GET
//string(6) "value1"
var_dump($coal->attribute2 ?? 'default');
//GET
// It should dump 'default' here. Instead, the error of __get() is triggered
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
GET
string(6) "value1"
string(7) "default"