class foo {
var $arr = array('I am A.', 'I am B.', 'I am C.');
var $r = 'I am r.';
}
$foo = new foo;
$arr = 'arr';
echo $foo->$arr[1];
It's order of precedence that's biting you here. The last line could be re-written as
echo $foo->{$arr[1]};
Since strings are arrays starting with index 0, $arr[1]
references the second character in 'arr', i.e. 'r', and results in printing the string 'I am r.'.
To get the results you're expecting, you need to explicitly tell PHP which part of the expression is meant as the variable name, because by default it greedily takes pretty much everything. Replacing the last line with this will get the output you expect:
echo $foo->{$arr}[1];
This will evaluate $arr
to its full string 'arr', and then use that as the label to the class member $arr
in foo
; the index will then grab the second entry in that array, 'I am B.'.