栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

SWOOLE 从入门到放弃之写个小框架(七)

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SWOOLE 从入门到放弃之写个小框架(七)

再继续。
大家看到了,我的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类也是用了单例模式,在这里说一下,用单例模式的目的就是希望在后续的开发中,可以很少限制的随便调用。并且,后续的框架开发中,我会尽可能的用单例模式,没别的原因,就是需要。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号