【发布时间】:2013-06-27 08:55:54
【问题描述】:
我目前正在开发一个 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{}
我认为没有别的办法,除非我一个一个地定义每个类......
【问题讨论】:
-
为什么不直接使用 $_SERVER?没有人真正使用 $GLOBALS
-
这里也一样,我赞成使用特定变量 $_SERVER、$_GET、$_POST 等。
-
我同意,这就像现在使用
$HTTP_GET_VARS而不是$_GET
标签: php arrays variables globals