|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-12-30 12:37 UTC] colder@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 01:00:01 2025 UTC |
Description: ------------ So, this is not really a bug itself but it's a problem. Let's say we have: class a { var $a = array('a'=>1); [...] some functions [...] }; Lets say a function inside the class does: var_dump($this->a); //this will dump $a array of class a. If we do var_dump($this->a['a']) it will dump integer 1. If we do: $z="a"; var_dump($this->$z); it will dump $a array of class a. Since here all is working fine because $z is used as a pointer. What happens with? $z="a"; var_dump($this->$z['a']); So, this will again dump the $a array in the class. The problem here is the following, because of the operator precedence, the index ['a'] is considered an index of $z which is a string. Then 'a' is casted to integer and interpreted as 0. $this->$z['a'] becomes the same as $this->$z[0] and, as $z[0] is 'a', this becomes $this->a (the array). This is not really a problem (even its not clear this precedence in the documentation as posted in other bug). My problem is that I couldn't find any way of telling the php interpreter that ['a'] should be considered as an index for the whole expression. ($this->$a)['a'] gives syntax error and I have been trying to find a way to do this without intermediate variables and couldn't figure out a solution. I know this example has no sense (who would really do this?) but still think there should be a way to specify the precedence (and if there is, it should be documentated in the operator precedence page, which lacks of the -> operator by the way). Thanks, Diego