|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-01-17 11:44 UTC] nobots dot nospam at example dot org
Description:
------------
The coalesce operator calls __get(prop) directly without testing isset($obj->prop) first.
It only calls isset($obj->prop) if there is an explicit user-defined __isset() handler.
Test script:
---------------
class Bar{
function __get($a){
throw new Error("no prop error\n");
}
}
$bar = new Bar;
try{
var_dump($bar->foo ?? "qux"); // error...
} catch(Throwable $e){ echo $e->getMessage(); }
var_dump(isset($bar->foo)); // ...but this works fine
Expected result:
----------------
class Bar{
function __isset($a){
return isset($this->{$a});
// I shouldn't be forced to implement this,
// since it looks exactly like the default isset() handler
}
function __get($a){
throw new Error("no prop error\n");
}
}
$bar = new Bar;
var_dump($bar->foo ?? "qux"); // works now, without errors...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 15:00:01 2025 UTC |
I found the following behavior odd. Is it expected as ewll? ``` class MagicProp { public function __isset($key) { return false; } public function __get($prop) { return 'yes'; } } $magic = new MagicProp(); return $magic->prop; // returns 'yes' return $magic->prop ?? 1; // returns 1 ```