You can expect the PHP engine to call __isset() before each property access is hidden in PHP. But this is not true . Check the documentation :
__ isset () is triggered by isset () or empty () call for inaccessible properties.
So this is the expected behavior as soon as:
var_dump(isset($c->a));
will call __isset() .
All other lines will just call __get() . And since you did not set these indexes, this is the expected behavior.
Change your code to:
class c { public $x = array(); public function __get($name) { var_dump(__METHOD__); return $this->x[$name]; } public function __isset($name) { var_dump(__METHOD__); return isset($this->x[$name]); } }
to see which methods are actually being called. This will give you:
c::__isset <------ called! bool(false) c::__get Notice: Undefined index: a in /tmp/a.php on line 6 Call Stack: 0.0002 647216 1. {main}() /tmp/a.php:0 0.0003 648472 2. c->__get() /tmp/a.php:16 <---------------------- not called bool(false) c::__get Notice: Undefined index: b in /tmp/a.php on line 6 Call Stack: 0.0002 647216 1. {main}() /tmp/a.php:0 0.0005 648656 2. c->__get() /tmp/a.php:17 <---------------------- not called bool(false) c::__get Notice: Undefined index: d in /tmp/a.php on line 6 Call Stack: 0.0002 647216 1. {main}() /tmp/a.php:0 0.0006 648840 2. c->__get() /tmp/a.php:18 <---------------------- not called bool(false)
source share