# Unexpected behavior of $this->$propertyName[...]

- Thread: https://externals.io/message/42645
- Original archive: https://news-web.php.net/php.internals/42645
- Started by Robert Lemke on 2009-01-15T19:24:01+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #42645 — Robert Lemke — 2009-01-15T19:24:01+00:00

URL: https://externals.io/message/42645#42645

Dear internals,

please consider the following code executed with PHP 5.3alpha3:

<?php
class Foo {
	protected $foo = array('bar' => 'baz');

	public function test() {
		$propertyName = 'foo';
		var_dump(isset($this->foo['bar']));
		var_dump(isset($this->$propertyName['bar']));
	}
}

$object = new Foo;
$object->test();
?>

Expected output:

array
   'foo' =>
     array
       'bar' => string 'quux' (length=4)

Actual output:

array
   'foo' =>
     array
       'bar' => string 'baz' (length=3)
   'f' => string 'quux' (length=4)

Is this a bug or missing feature?

Cheers,
robert

---

## #42646 — Paul Biggar — 2009-01-15T19:35:37+00:00

URL: https://externals.io/message/42645#42646 · In reply to: #42645

> Dear internals,
>
> please consider the following code executed with PHP 5.3alpha3:

I suspect neither bug nor feature. I think you expect that
     $this->$propertyName['bar']
 is the same as:
     ($this->$propertyName)['bar']
but in fact it is:
     $this->($propertyName['bar'])

So in your example, 'bar' is the string index 0 for "foo"[0], hence 'f'.


> Is this a bug or missing feature?

It has been pointed out before that one should be able to index an
expression, but currently PHP can only index variables. Personally, it
seems like a bug, but I'm not sure whether or not there's consensus.

Paul


-- 
Paul Biggar
paul.biggar@gmail.com

---

## #42649 — Robert Lemke — 2009-01-15T20:21:04+00:00

URL: https://externals.io/message/42645#42649 · In reply to: #42646

Hi Paul,

Am 15.01.2009 um 20:35 schrieb Paul Biggar:

> I suspect neither bug nor feature. I think you expect that
>     $this->$propertyName['bar']
> is the same as:
>     ($this->$propertyName)['bar']
> but in fact it is:
>     $this->($propertyName['bar'])
>
> So in your example, 'bar' is the string index 0 for "foo"[0], hence  
> 'f'.

okay, I already suspected something like that but had already suppressed
that $string[0] works equally as $string{0}.

> It has been pointed out before that one should be able to index an
> expression, but currently PHP can only index variables. Personally, it
> seems like a bug, but I'm not sure whether or not there's consensus.

IMO it feels like a bug, although I now understand the reasons.

What was the reason again to deprecate curly brackets access in PHP6? It
would make the situation clearer in the above example if square brackets
would be deprecated instead ...

Anyway, thank you for the clarification.

Robert

---

## #42652 — Etienne Kneuss — 2009-01-15T23:04:36+00:00

URL: https://externals.io/message/42645#42652 · In reply to: #42645

Hello,


> Dear internals,
>
> please consider the following code executed with PHP 5.3alpha3:
>
> <?php
> class Foo {
>        protected $foo = array('bar' => 'baz');
>
>        public function test() {
>                $propertyName = 'foo';
>                var_dump(isset($this->foo['bar']));
>                var_dump(isset($this->$propertyName['bar']));
>        }
> }
>
> $object = new Foo;
> $object->test();
> ?>
>
> Expected output:
>
> array
>  'foo' =>
>    array
>      'bar' => string 'quux' (length=4)
>
> Actual output:
>
> array
>  'foo' =>
>    array
>      'bar' => string 'baz' (length=3)
>  'f' => string 'quux' (length=4)
>
> Is this a bug or missing feature?

It's a simple matter of precedence.
You want $this->{$propertyName}['bar']; here

Regards

>
> Cheers,
> robert
>
>
> --
> 
> 
>
>
>
>



-- 
Etienne Kneuss
http://www.colder.ch

Men never do evil so completely and cheerfully as
when they do it from a religious conviction.
-- Pascal
