私は現在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つずつ定義しない限り、他に方法はないと思います...