所以正常的类方法和对象创建就像这样......
$obj = new Class();
$obj2 = Class::someMethod();
我可以动态实例化“class”吗?怎么样?我想做点什么...
$class = "Class";
$obj = new $class?;
$obj2 = $class?::someMethod();
答案 0 :(得分:5)
$class = "Class";
$obj = new $class; // works
$obj2 = $class::someMethod(); // works as of PHP 5.3
// if you don't have PHP 5.3 and want to use the ladder:
$obj2 = call_user_func(array($class, 'someMethod'));
答案 1 :(得分:1)
?
中没有 $obj = new $class?
的应该有效。