php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55021 Multiple bug on variable variable error parse/comportement
Submitted: 2011-06-10 02:07 UTC Modified: 2011-07-21 12:26 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: dev at meta-gen dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3SVN-2011-06-09 (snap) OS: Debian lenny/SID
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dev at meta-gen dot com
New email:
PHP Version: OS:

 

 [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...


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-20 23:24 UTC] binarycleric at gmail dot com
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".
 [2011-07-21 09:25 UTC] dev at meta-gen dot com
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.
 [2011-07-21 11:46 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-07-21 11:46 UTC] rasmus@php.net
You are a bit confused. Forget all the class stuff. 
Just look at:
$b='a';
echo $b['key'];

You will see that prints: a

That's because $b is a string and not an array and therefore only takes integer 
offsets. (int)'key' is 0, so your code is the same as doing $b[0] which means 
you will never see 'key' in your result and also since it isn't an array you 
can't append to it with [].
 [2011-07-21 12:26 UTC] dev at meta-gen dot com
Yes i see $b[$k] not evaluate, 
sure it's ambiguous notation like $this->$b work;

Im sorry for a bad repport post.

I appreciate the attention, especially rasmus.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 05 12:01:34 2025 UTC