I am unable to understand variable Parsing.
I have gone through this link uniform_variable_syntax but this link is so hard me for understand.
class Test{
public $var=array('baz'=>'a');
function a(){
return 'amazing_class<br/>';
}
}
function a(){
return 'amazing_out_of_class<br/>';
}
$obj=new Test();
$bar='var';
echo "1. ".$obj->$bar['baz'](); //Output amazing_out_of_class
$bar=array('baz'=>'a');
echo "2. ".$obj->{$bar['baz']}(); //Output amazing_class
Now lets look at first case : $obj->$bar['baz']()
($obj->$bar)['baz']()
>>> ($obj->var['baz'])() >>> a()
>>> amazing_out_of_class
Now i also suppose it $obj->{$bar['baz']}()
parsed same as above and expected Notice error : undefined Property a
$obj->{$bar['baz']}()
>>> ($obj->a)() >>> ($obj->a) is Notice error : undefined Property a
Notice error : undefined Property a
is my assumption according to first case but its output amazing_class
Its pretty simple
$bar='var';
echo "1. ".$obj->$bar['baz'](); //Output amazing_out_of_class
For example $obj->$bar'baz' evaluates as bellow
$obj->$bar['baz']() -> $obj->$var['baz']() -> {$obj->a} () -> a()
Second One also evaluates as a
$obj->{$bar['baz']}() -> $obj->{a}() ( $bar['baz'] as a)
Reference :http://php.net/manual/en/language.variables.php php.net/manual/en/language.types.string.php
Please add, if anything is missing
$bar=array('baz'=>'a');
echo "2. ".$obj->{$bar['baz']}();
May be you need to change from
$obj->$bar['baz']();
To
$obj->{$bar}['baz']();
Final code would be as
$bar='var';
echo "1. ".$obj->{$bar}['baz'](); //Output amazing_out_of_class
$bar=array('baz'=>'a');
echo "2. ".$obj->{$bar['baz']}(); //Output amazing_class
Live demo : https://eval.in/915826