我在这方面有些挣扎,希望能有所帮助.
在PHP中,变量可以像这样轻松定义
$a = "myVar";
$$a = "some Text";
print $myVar; //you get "some Text"
现在,如何在OOP环境中做到这一点?我尝试了这个:
$a = "myVar";
$myObject->$a = "some Text"; //I must be doing something wrong here
print $myObject->myVar; //because this is not working as expected
我也尝试了$myObject-> {$a} =“ some Text”,但是它也不起作用.所以我一定在某个地方很误会.
谢谢你的帮助!
解决方法:
这对我有用:
class foo {
var $myvar = 'stackover';
}
$a = 'myvar';
$myObject = new foo();
$myObject->$a .= 'flow';
echo $myObject->$a; // prints *