您当前的位置: 首页 >  php

苗先生的PHP记录

暂无认证

  • 0浏览

    0关注

    190博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

PHP封装一套自己的链式操作

苗先生的PHP记录 发布时间:2022-05-13 21:00:09 ,浏览量:0

这里我直接在控制器中实现的 , 需要自己做一下类的抽取

这里参照的某框架的链式操作思想

思想是通过一个条件或者一个方法, 进行一个结构的判断

然后通过clone, 把这个类返回出去

达到链式操作的样子

 /**
     * @RequestMapping("/ger")
     */
    public function ins(Request $request)
    {
        return $this->if($request->isGet())
            ->then(function (){
                return "get请求";
            })
            ->if($request->isPost())
            ->then(function (){
                return "post请求";
            })
            ->getRes();
   }

    private $bools;   private $res;
    private function if(bool $bool=false)
    {
        $this->bools = $bool;
        return clone $this;
   }

    private function then(callable $fun)
    {
        if($this->bools){
            $this->res = $fun();
        }
        return clone $this;
   }

    private function getRes()
    {
        return $this->res;
   }

关注
打赏
1665468453
查看更多评论
立即登录/注册

微信扫码登录

0.0359s