【发布时间】:2015-05-22 23:28:58
【问题描述】:
运行 PHP 5.4,所以我没想到会这样,但我遇到了以下错误:
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
假设你有一个stdClass的变量设置如下:
$this->variable = new stdClass();
$this->variable->other = array('class' => 'helloworld');
现在,假设您要访问 helloworld 类的静态方法:
// Standard call
$x = helloworld::my_static_method();
// Call with variable class name
$x = $this->variable->other['class']::my_static_method();
当使用变量类名调用上述内容时,我收到解析错误。奇怪的是,如果我执行以下操作,则不会出现错误:
$class = $this->variable->other['class'];
$x = $class::my_static_method();
在我看来,这似乎很奇怪,谁能想到在使用第一个示例与第二个示例时类名无法正确解析的原因?
【问题讨论】:
标签: php