PHP:如何使用存储在成员字符串中的名称创建类的新对象?

时间:2011-08-25 12:18:41

标签: php reflection

所以我想做这样的事情:

$ob = new $this->other_class_name;

但它失败了。如果不将other_class_name存储在局部变量中,我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

将名称保存在另一个变量中:

$class = $this->other_class_name;
$ob = new $class;

答案 1 :(得分:1)

试试这个:

$class = get_class($this->other_class_name);
$ob = new $class;
相关问题