php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43702 Operator precedence problem
Submitted: 2007-12-29 09:05 UTC Modified: 2007-12-30 12:37 UTC
From: gdiego at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.5 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gdiego at gmail dot com
New email:
PHP Version: OS:

 

 [2007-12-29 09:05 UTC] gdiego at gmail dot com
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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-12-30 12:37 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

$a->{$z}['a']
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 02 00:01:30 2024 UTC