1

私は現在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{}

すべてのクラスを1つずつ定義しない限り、他に方法はないと思います...

4

3 に答える 3

0

$_SERVER を使用して問題を解決する

于 2013-06-29T20:08:51.460 に答える