我目前正在开发一个 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{}
我认为没有其他办法,除非我一一定义每个类......
最佳答案
我找到了! 问题出在 php.ini 文件中
选项
auto_globals_jit = 开 应为“关闭”
关于php $GLOBALS 显示未定义 "_SERVER",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384107/