【问题标题】:php $GLOBALS show undefined "_SERVER"php $GLOBALS 显示未定义的“_SERVER”
【发布时间】: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


【解决方案1】:

我找到了! 问题出在 php.ini 文件中

选项

auto_globals_jit = 开启 应该是“关闭”

【讨论】:

    【解决方案2】:

    检查var_dump($GLOBALS) - 也许你在某处覆盖了你的 $GLOBALS。

    【讨论】:

    • var_dump($GLOBALS);给出 array(6) { ["_GET"]=> array(0) { } ["_POST"]=> array(0) { } ["_COOKIE"]=> array(1) { ["PHPSESSID"]= > string(26) "52l9aj4ql1672jik19f1pdi647" } ["_FILES"]=> array(0) { } ["GLOBALS"]=> RECURSION ["_SESSION"]=> &array(0) { } }
    • 如果 $GLOBALS['_SERVER'] 在开头确实存在并且在其他地方不存在,您应该找到它消失的那一行。也许像 unset($_SERVER) 之类的?
    【解决方案3】:

    使用 $_SERVER 并解决问题

    【讨论】:

    • 有时候就是这么简单。
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多