I understand the concept of variable variables in PHP. According to my understanding of variable variables in PHP the following code:
$foo = 'hello';
$$foo = 'world';
echo $foo . ' ' . $hello;
Will generate the output as:
Hello World
But I am finding it difficult to understand the case of variable object properties
Say I have a class called foo with a single property as follows:
class foo {
var $r = 'I am r.';
}
Now, creating the instance of the class foo and using the concept of variable variables the following code:
$foo = new foo();
$bar = 'r';
echo $foo->$bar;
will output:
I am r.
Up till everything is fine, but when I include a property having an array value it gets messed up for me. Say for example I add another property with array value to the class foo and now the class looks like:
class foo {
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}
Now When I create the instance of the class foo and try to read the property $arr[1] as follows:
$arr = 'arr';
echo $foo->$arr[1];
Which outputs:
I am r.
and the output looks weird to me. I mean how does $foo->$arr[1] resolves to the propery $r in the class foo ?
Shouldn't doing $foo->$arr resolve into $foo->arr[1] (Note: There is no dollar sign) giving the output:
I am B.
How does this happen?
I am aware that doing $foor{$arr}[1] outputs "I am B."
My question is why does $foo->$arr doesn't resolve into $foo->arr ? Given that the variable $arr is holding the value 'arr' ?
A note to admins/supervisors: This question is not a duplicate; I have tried searching for the similar questions but none of it answered what I need to know.