【发布时间】:2011-01-05 20:31:19
【问题描述】:
是否可以动态访问对象的子属性?我管理它来访问对象的属性,而不是子对象的属性。
这是我想做的事情的一个例子:
class SubTest
{
public $age;
public function __construct($age)
{
$this->age = $age;
}
}
class Test
{
public $name;
public $sub;
public function __construct($name, $age)
{
$this->name = $name;
$this->sub = new SubTest($age);
}
}
$test = new Test("Mike", 43);
// NOTE works fine
$access_property1 = "name";
echo $test->$access_property1;
// NOTE doesn't work, returns null
$access_property2 = "sub->age";
echo $test->$access_property2;
【问题讨论】:
标签: php oop class properties