Edit report at https://bugs.php.net/bug.php?id=55021&edit=1
ID: 55021
Comment by: binarycleric at gmail dot com
Reported by: dev at meta-gen dot com
Summary: Multiple bug on variable variable error
parse/comportement
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Debian lenny/SID
PHP Version: 5.3SVN-2011-06-09 (snap)
Block user comment: N
Private report: N
New Comment:
I looked into this for a bit and I must say I was a bit confused until the
answer smacked me in the face.
The 'fn3' method isn't doing what you think it's doing. Upon glancing at the
code it may appear that $this-
>a['first'] is having the value 'foo' appended to it (which doesn't make sense
because $this->a['first'] is
a string, not an array).
What is actually happening (according to my tests on PHP 5.2.17) is that $b[$k]
[] is being evaluated first
and then the result is being used at the property name, which explains the
"Cannot use [] for reading"
error.
Assuming your test code was changed and $this->a['first'] was an array, the
"slightly more correct" way
would be to call $this->{$b}[$k][] which would evaluate to $this->a['first'][].
In my opinion this should be classified as "not a bug".
Previous Comments:
------------------------------------------------------------------------
[2011-06-10 02:07:09] dev at meta-gen dot com
Description:
------------
When use variable variable with property array in class, a multipe bug occur.
On function exemple fn, variable variable cast in string
On function exemple fn3, parse error 'Cannot use [] for reading in'
Test script:
---------------
class C
{
public $a = array('first' => 'baz');
/**
* It's work but $this->a is cast in string and override array
* @note $this->a : string(3) "foo"
*/
public function fn($k)
{
$b = 'a';
$this->$b[$k] = 'foo';
}
/**
* Normaly use
* @note Similar result of fn
* @note $this->a : string(3) "foo"
*/
public function fn2($k)
{
$b = 'a';
$this->$b = 'foo';
}
/**
* This case is most interesting, because as it run parse error
* @note don't parse $this->$b
*/
public function fn3($k)
{
$b = 'a';
$this->$b[$k][] = 'foo'; ///BUG Error parse : PHP Fatal error:
Cannot use [] for reading in ....
}
/**
* Solution for fix fn3
*/
public function fn4($k)
{
$b = 'a';
$tmp =& $this->$b;
$tmp[$k][] = 'foo';
}
}
Expected result:
----------------
for function fn('key') : array('first' => 'baz', 'key' => 'foo')
for function fn3('key') :
array(2) {
["first"]=>
string(3) "baz"
["key"]=>
array(1) {
[0]=>
string(3) "foo"
}
}
Actual result:
--------------
for function fn('key') : string(3) "foo"
for function fn3('key') : Cannot use [] for reading in...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55021&edit=1