【发布时间】:2016-12-20 16:05:32
【问题描述】:
从 PHP 5.3 开始,可以使用变量作为类名,不仅用于对象实例化,还用于even for static methods as well:
$className = 'My\Name\Spaced\Thing';
$thing = $className::foo('hello world');
但是,如果我尝试使用函数或方法的返回值而不是实际变量,则会出现错误:
function getClassName()
{
return 'My\Name\Spaced\Thing';
}
// Raises "syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)"
$thing = getClassName()::foo('hello world');
另一方面,这很好用:
$className = getClassName();
$thing = $className::foo('hello world');
什么给了?我是不是在 PHP (5.6) 的语法处理器中发现了一个错误?
【问题讨论】:
标签: php syntax syntax-error late-static-binding