|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-06-10 02:07 UTC] 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...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 24 23:00:01 2025 UTC |
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".Hello, Sorry perhaps i not good explain, I found TWO bug. The first is in function 'fn' I try add a value in $this->a by variable variable like this : class C { public $a = array(); public function fn() { $b = 'a'; $this->$b['key'] = 'foo'; return $this->a; } } $C = new C; var_dump($C->fn()); This code return string(3) "foo". IS BAD RESULT Normaly GOOD result for fn must be array(1) { ["key"]=> string(3) "foo" } because $this->$b == $this->a AND second bug is in fn3 class C { public $a = array(); public function fn3() { $b = 'a'; $this->$b['key'][] = 'foo'; ///BUG Error parse : PHP Fatal error: Cannot use [] for reading in .... return $this->a; } } $C = new C; var_dump($C->fn3()); This code cause Error parse : PHP Fatal error: Cannot use [] for reading in .... when it should return array(1) { ["key"]=> array(1) { [0]=> string(3) "foo" } } because $this->$b['key'][] == $this->a['key'][] Salutation.