T_PAAMAYIM_NEKUDOTAYIM听起来很奇特,但对我来说绝对是胡说八道.我将这一切追溯到这行代码:
<?php
Class Context {
protected $config;
public function getConfig($key) { // Here's the problem somewhere...
$cnf = $this->config;
return $cnf::getConfig($key);
}
function __construct() {
$this->config = new Config();
}
}
?>
Run Code Online (Sandbox Code Playgroud)
在构造函数中,我创建了一个Config对象.这是班级:
final class Config {
private static $instance = NULL;
private static $config;
public static function getConfig($key) {
return self::$config[$key];
}
public static function getInstance() {
if (!self::$instance) {
self::$instance = new Config();
}
return self::$instance;
}
private function __construct() {
// include configuration file
include __ROOT_INCLUDE_PATH . '/sys/config/config.php'; // defines a $config array
$this->config = $config;
}
}
Run Code Online (Sandbox Code Playgroud)
不知道为什么这不起作用/错误意味着什么......
ben*_*ley 68
T_PAAMAYIM_NEKUDOTAYIM是PHP使用的双冒号范围解析 - ::
快速浏览一下你的代码,我想这一行:
return $cnf::getConfig($key);
Run Code Online (Sandbox Code Playgroud)
应该
return $cnf->getConfig($key);
Run Code Online (Sandbox Code Playgroud)
第一种是静态调用方法的方法 - 如果$ cnf包含一个也是有效类的字符串,则此代码有效. - >语法用于在类/对象的实例上调用方法.
tom*_*fen 11
对于有这个问题的未来访客来说,这只是我的两分钱.
这是PHP 5.3的正确语法,例如,如果从类名调用静态方法:
MyClassName::getConfig($key);
Run Code Online (Sandbox Code Playgroud)
如果您以前将ClassName分配给$ cnf变量,则可以从中调用静态方法(我们讨论的是PHP 5.3):
$cnf = MyClassName;
$cnf::getConfig($key);
Run Code Online (Sandbox Code Playgroud)
但是,这个sintax在PHP 5.2或更低版本上不起作用,您需要使用以下内容:
$cnf = MyClassName;
call_user_func(array($cnf, "getConfig", $key, ...otherposibleadditionalparameters... ));
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助人们在5.2版本中出现此错误(不知道这是否是openfrog的版本).