再继续。
大家看到了,我的App.php文件是用了单例模式。这么做有一个好处就是不会被大量的实例化,能节省内存。也是为了能满足我之前立的FLAG。
继续撸代码
我希望尽可能少的用全局变量,比如$_GET,$_POST之类的。反正SWOOLE已经提供了很好的方法,为什么不用它呢,咱们接下来先搞搞$request。
创建文件 frame/Lib/Request.php,贴
server= $request->server;
$this->header= $request->header;
$this->tmpfiles = $request->tmpfiles;
$this->request = $request->request ;
$this->cookie= $request->cookie ;
$this->get = $request->get ;
$this->files = $request->files ;
$this->post = $request->post ;
$this->rawContent = $request->rawContent();
$this->getData = $request->getData();
}
//变身后它就不是废物了,那就得让小伙伴们能使用它,这里使用了这么一个魔术方法。
public function __get($name){
return $this->$name;
}
}
试一下它好不好使。直接改frame/Lib/App.php代码
public function http($request,$response){
$req = Request::get_instance ();
$req->set($request);
$response->end(var_export ($req->server,TRUE));
}
运行start.php, 浏览器查看
很无奈,它成功了。。
Request类也是用了单例模式,在这里说一下,用单例模式的目的就是希望在后续的开发中,可以很少限制的随便调用。并且,后续的框架开发中,我会尽可能的用单例模式,没别的原因,就是需要。



