note 44218 added to language.types.object

From: Date: Sun, 18 Jul 2004 16:43:41 +0000
Subject: note 44218 added to language.types.object
Groups: php.notes 
Request: Send a blank email to php-notes+get-73488@lists.php.net to get a copy of this message
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(); ?> ---- Manual Page -- http://www.php.net/manual/en/language.types.object.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44218 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44218&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44218&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#73488) next »