【发布时间】:2011-01-06 03:44:29
【问题描述】:
在类中调用静态函数时,我在使用变量作为类名时遇到问题。我的代码如下:
class test {
static function getInstance() {
return new test();
}
}
$className = "test";
$test = $className::getInstance();
我必须将类名定义为变量,因为类名来自数据库,所以我永远不知道要创建哪个类的实例。
注意:目前我收到以下错误:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
谢谢
【问题讨论】:
-
有趣的是,您的代码在 PHP 5.3.1 中适用于我并且不会引发错误。
-
可变静态类在 PHP 5.3+ 中可用,任何更低的都需要 @hobodave 提到的 call_user_func() / call_user_func_array()
标签: php static-methods instantiation php-5.2