【发布时间】:2016-12-28 04:58:50
【问题描述】:
在下面的类上调用Config::get('modules');怎么会出现上述错误?
如果我只返回static::$config,该函数可以正常工作,但是当我尝试返回它的元素时,我得到了错误,即使它已经明确定义了。
class Config
{
private static $config = NULL;
private static $initialized = FALSE;
public static function _init()
{
if(self::$initialized == TRUE)
{
return;
}
static::$config = $GLOBALS['config'];
unset($GLOBALS['config']);
var_dump(static::$config);
static::$initialized = TRUE;
}
public static function get($property = '')
{
self::_init();
var_dump(static::$config);
$parts = explode('.', $property);
$path = 'config';
foreach($parts as $part)
{
$path .= '["'.$part.'"]';
}
return static::$$path;
}
}
函数和错误中 var 转储的输出。
array(3) {
["APP_VERSION"]=>
string(5) "0.0.1"
["database"]=>
array(3) {
["dsn"]=>
string(32) "mysql:host=localhost;dbname=test"
["user"]=>
string(4) "root"
["pass"]=>
string(0) ""
}
["modules"]=>
array(0) {
}
}
array(3) {
["APP_VERSION"]=>
string(5) "0.0.1"
["database"]=>
array(3) {
["dsn"]=>
string(32) "mysql:host=localhost;dbname=test"
["user"]=>
string(4) "root"
["pass"]=>
string(0) ""
}
["modules"]=>
array(0) {
}
}
Fatal error: Access to undeclared static property: Config::$config["modules"] in C:\xampp\htdocs\project\system\classes\Config.php on line 37
【问题讨论】:
-
PHP 正在寻找一个名为
config["modules"]的变量。它只是不能那样工作。
标签: php arrays class properties static