我目前正在开发一个 php 框架,我必须在其中存储$_SERVER
, $_GET
,$_POST
等的变量...
但是当我打电话时,$GLOBALS["_SERVER"]
我得到未定义的索引错误。这是不可能的,因为$_SERVER
它是一个预定义的变量.. 对吗?
但是当我$_SERVER
在代码的开头调用时$GLOBALS["_SERVER"]
是定义的。
你们都搞错了我想使用 $GLOBALS 因为以下类,
class Base_Infrastructure{
function __construct(){
$name = '_'.strtoupper(str_replace(__NAMESPACE__."\\","",get_called_class()));
foreach($GLOBALS[$name] as $i => $v){
$this->{$i} = $v;
}
}
private function dispatch(){
$name = '_'.strtoupper(str_replace(__NAMESPACE__."\\","",get_called_class()));
$GLOBALS[$name] = $this;
return true;
}
function get($name){
if(isset($this->{$name})&&!empty($this->{$name})){
return $this->{$name};
}
}
function set($name,$value){
$this->{$name} = $value;
$this->dispatch();
return true;
}
function remove($name){
unset($this->{$name});
$this->dispatch();
return true;
}
}
class Server extends Base_Infrastructure{}
class Post extends Base_Infrastructure{}
class Get extends Base_Infrastructure{}
class Session extends Base_Infrastructure{}
class Cookie extends Base_Infrastructure{
function set($name,$value){
$config = $GLOBALS['CONFIG']['cookie'];
$this->{$name} = $value;
setcookie($name,$value,time()+$config['expire'],$config['path']);
}
function remove($name){
$config = $GLOBALS['CONFIG']['cookie'];
unset($this->{$name});
setcookie($name,null,time()-$config['expire'],$config['path']);
}
}
class Files extends Base_Infrastructure{}
我认为没有其他办法,除非我一个一个地定义每个类......