PHP实用小技巧之调用录像的方法
php  /  管理员 发布于 7年前   126
主要功能 把你实际的调用操作录下来,然后在你想要的地方重新调用 和匿名函数的作用基本一样,暂存你的调用操作 一般用于链式调用, 然后实际作用于你想要操作的对象上面 使用场景 假如 laravel 项目用到了 仓库模式, 然后对于比较复杂的查询条件,一般情况下有三种操作 现在可以对第三种方法进行优化,传入一个下面代码里的 CallEcho 对象 看着和匿名函数差不多,但是 CallEcho 可以被继承来实现些接口,继承后还可以对查询条件进行一些操作,比如过滤什么的。用匿名函数的话,完全就看调用方的良心了。 上代码 简单的测试 以及 使用 PS 灵感来源于slim3 中间件 的实现 总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对AIDI的支持。好像和没说一样//控制器里$callEcho = (new CallEcho())->where("username", "马云")->where("is_boss", 1)->first();$fubao = (new UserRepository)->first($callEcho);//仓库类class UserRepository{ public function first(CallEcho $callEcho){ return $callEcho->invoke(new User()); }}
最重要的是不传对象传函数叫什么面对对象class CallEcho{ protected $callable = null; public function __construct() { //callable 初始化 $this->seed(); } protected function seed(){ $this->callable = $this; } public function __invoke($obj) { return $obj; } public function __call($name, $arguments) { $current = $this->callable; /** * 每产生__call,就往callable上面裹一层 */ $this->callable = function($obj) use($name, $arguments, $current){ return call_user_func_array($current, [$obj])->{$name}(...$arguments); }; return $this; } //作用于真正的对象上面 public function invoke($obj){ return call_user_func_array($this->callable, [$obj]); }}
class TestCallEcho{ protected $called = []; public function __call($name, $arguments) { $this->called[] = [$name, $arguments]; return $this; } public function end(){ $this->called[] = "end"; return $this; } public function getCalled(){ return $this->called; }}function testArrayEq($array1, $array2){ if(count($array1) !== count($array2)){ return false; } foreach ($array1 as $index => $value1){ if(!isset($array2[$index])){ return false; } $value2 = $array2[$index]; if(is_array($value1) && is_array($value2)){ if(!testArrayEq($value1, $value2)){ return false; }else{ //继续判断 } }else{ if($value1 !== $value2){ return false; } } } return true;}function testTestArrayEq(){ $array1 = [1, 2]; $array2 = [1, 3]; $array3 = [1, 2, 3]; assert(testArrayEq($array1, $array2) == false); assert(testArrayEq($array1, $array3) == false); assert(testArrayEq($array1, $array1) == true);}testTestArrayEq();$obj = new \stdClass();$callEcho = new CallEcho();/*************重点开始****************//** @var CallEcho $callEcho */$callEcho = $callEcho->testNumber(1)->testString("myname")->testObj($obj)->testMulti(1, "myname")->testMulti2("1", $obj)->end();/** @var TestCallEcho $testCallEcho */$testCallEcho = $callEcho->invoke(new TestCallEcho());/************重点结束****************///基本上和这个作用一样$a = function($obj){ $obj->testNumber(1)->testString("myname")->testObj($obj)->testMulti(1, "myname")->testMulti2("1", $obj)->end();};$called = $testCallEcho->getCalled();$eq = testArrayEq($called, [ ["testNumber", [1]], ["testString", ["myname"]], ["testObj", [$obj]], ["testMulti", [1, "myname"]], ["testMulti2", ["1", $obj]], "end"]);assert($eq);
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号