note 44218 deleted from language.types.object by cmb

From: Date: Fri, 15 May 2015 19:14:35 +0000
Subject: note 44218 deleted from language.types.object by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-202548@lists.php.net to get a copy of this message
Note Submitter: nconantj ---- php at electricsurfer.com, More than a year later and here's some clarification of what's happening in your code, via comments in an otherwise verbatim copy. <? class c { var $a = array('a'=>'aa','b'=>'ab'); var $b = 'c'; function show() { echo $this->a['a']; // -> 1st echo $this->a['b']; // outputs 'ab' $a = 'a'; $b = 'b'; echo $this->$a[$a]; // [] 1st, not what I expected //Above first becomes $this->$a['a'] by looking at the function's local $a //Next it becomes $this->a by again looking at the function's local $a, which references the class variable $a with no subscripts. // In order to reference elements of the class variable $a, // you want to use $this->a[$a] echo $this->$a[$b]; // does NOT output 'ab' // Same as above, but the first step $b becomes 'b' $this_a =& $this->$a; // work-around echo $this_a[$a]; // no question echo $this_a[$b]; $a_arr = array('a'=>'b'); echo $this->$a_arr[$a]; // [] 1st => outputs 'c' // This becomes $this->$a_arr['a'] which becomes $this->c, // by referencing the local variables first. } } $c = new c(); $c->show(); ?>

« previous php.notes (#202548) next »