//读取与设置配置项 (这里的注释的意思就读取和设置。我是在想不通是什么设置和读取的啦谢谢大神!!!)
- PHP code
function C($name=null,$value=null){
static $config=array();//配置项
if(is_null($name)){
return $config;
}
if(!is_null($name)){
if(is_string($name)){
$name = strtolower($name);
if(is_null($value)){
return isset($config[$name])?$config[$name]:false;
}
$config[$name]=$value;
}elseif(is_array($name)){
$name = array_change_key_case($name,CASE_LOWER);
$config = array_merge($config,$name);
}
}
}
------解决方案--------------------
妙处在于 static $config 的使用,当离开函数作用域事它的值并不会消失,而会保留最近一次的值。
这样看就清楚了:
- PHP code
c("hello", "I say hello"); //一次赋值
c(array("a", "b")); //二次赋值
var_dump( c());
array(3) {
["hello"]=>
string(11) "I say hello"
[0]=>
string(1) "a"
[1]=>
string(1) "b"
}