Bug #53750 [Opn->Bgs]: dynamic variable name problem
| From: | aharvey@php.net | Date: | Mon, 17 Jan 2011 04:12:49 +0000 |
| Subject: | Bug #53750 [Opn->Bgs]: dynamic variable name problem | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-157944@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=53750&edit=1
ID: 53750
Updated by: aharvey@php.net
Reported by: tuhin dot barua at gmail dot com
Summary: dynamic variable name problem
-Status: Open
+Status: Bogus
Type: Bug
-Package: Arrays related
+Package: Scripting Engine problem
Operating System: ubuntu
PHP Version: 5.2.17
Block user comment: N
Private report: N
New Comment:
This is a simple operator associativity issue: $a->$arr[0] is actually
parsed as $a->{$arr[0]}, and since the first character of $arr is "a",
the last line in the test script ends up effectively being
print_r($a->a).
You can work around this by changing the last line of the script to:
print_r($a->{$arr}[0]);
Previous Comments:
------------------------------------------------------------------------
[2011-01-14 17:58:00] tuhin dot barua at gmail dot com
Description:
------------
example 1:
php > $a = new stdclass();
php > $a->arr = array(1,2,3);
php > $arr = 'arr';
php > print_r($a->$arr);
Array
(
[0] => 1
[1] => 2
[2] => 3
)
php > print_r($a->$arr[0]);
php >
example 2:
php > $arr = array(1,2,3);
php > $a = 'arr';
php > print_r($$a);
Array
(
[0] => 1
[1] => 2
[2] => 3
)
php > print_r($$a[0]);
arr
php > print_r($$a[1]);
php >
Test script:
---------------
$a = new stdclass();
$a->arr = array(1,2,3);
$arr = 'arr';
print_r($a->$arr);
print_r($a->$arr[0])
Expected result:
----------------
cant access array elements with a dynamic variable name.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53750&edit=1