我有档案init.php
:
<?php
require_once 'config.php';
init::load();
?>
与config.php
:
<?php
$config = array('db'=>'abc','host'=>'xxx.xxx.xxx.xxxx',);
?>
名称为的课程something.php
:
<?php
class something{
public function __contruct(){}
public function doIt(){
global $config;
var_dump($config); // NULL
}
}
?>
为什么它为空?
在php.net中,他们告诉我可以访问,但实际上不能访问。我尝试过,但不知道。我正在使用PHP 5.5.9。
$config
in中的变量config.php
不是全局变量。
要使其成为全局变量,我不建议您global
在它前面写一个神奇的词。
我建议您阅读超全局变量。
还有一点可变范围。
我建议创建一个可以处理您这个问题的课程。
看起来应该像
class Config
{
static $config = array ('something' => 1);
static function get($name, $default = null)
{
if (isset (self::$config[$name])) {
return self::$config[$name];
} else {
return $default;
}
}
}
Config::get('something'); // returns 1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句