php - How to implements _isset magic method? -


i try implement __isset magic method such following code,

why undefined index error? can tell me how do?

class c {     public $x = array();     public function __get($name) {         return $this->x[$name]; //undefined index: #1:a / #2:b / #3:d     }     public function __isset($name) {         return isset($this->x[$name]);     } }  $c = new c; var_dump(isset($c->a));       var_dump(isset($c->a->b));   #1 var_dump(isset($c->b->c));   #2 var_dump(isset($c->d['e'])); #3 

why following code working fine? don't understand?

$x = array(); var_dump(isset($x->a->b->c));        var_dump(isset($x['a']['b']['c'])); 

you might expect, php engine call __isset() before every access hidden properties in php. thats not true. check documentation:

__isset() triggered calling isset() or empty() on inaccessible properties.

so, that's expected behaviour, only:

var_dump(isset($c->a)); 

will trigger __isset().

all of other lines trigger __get(). , didn't set indexes expected behaviour.

change 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 methods called. give you:

c::__isset   <------ called! bool(false) c::__get  notice: undefined index: 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) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -