It is not possible to do
$str = 'FooBar->foo'; /* or */ $str = 'FooBar::foo';
echo $$str;
because PHP would have to evaluate the operation to the object or class first. A variable variable takes the value of a variable and treats that as the name of a variable. $FooBar->foo
is not a name but an operation. You could do this though:
$str = 'FooBar';
$prop = 'foo';
echo $$str->$prop;
From PHP Manual on Variable Variables:
Class properties may also be accessed using variable property names. The variable property name will be resolved within the scope from which the call is made. For instance, if you have an expression such as $foo->$bar, then the local scope will be examined for $bar and its value will be used as the name of the property of $foo. This is also true if $bar is an array access.
Example from Manual:
class foo {
var $bar = 'I am bar.';
}
$foo = new foo();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $foo->$bar . "\n";
echo $foo->$baz[1] . "\n";
Both echo
calls will output "I am bar".