主要功能
把你實際的調(diào)用操作錄下來,然后在你想要的地方重新調(diào)用
和匿名函數(shù)的作用基本一樣,暫存你的調(diào)用操作 一般用于鏈式調(diào)用, 然后實際作用于你想要操作的對象上面
好像和沒說一樣
使用場景
假如 laravel 項目用到了 倉庫模式, 然后對于比較復雜的查詢條件,一般情況下有三種操作
現(xiàn)在可以對第三種方法進行優(yōu)化,傳入一個下面代碼里的 CallEcho 對象
//控制器里 $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()); } }
看著和匿名函數(shù)差不多,但是 CallEcho 可以被繼承來實現(xiàn)些接口,繼承后還可以對查詢條件進行一些操作,比如過濾什么的。用匿名函數(shù)的話,完全就看調(diào)用方的良心了。
最重要的是不傳對象傳函數(shù)叫什么面對對象
上代碼
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; /** * 每產(chǎn)生__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{ //繼續(xù)判斷 } }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);
PS
靈感來源于slim3 中間件 的實現(xiàn)
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
標簽:婁底 銅川 鎮(zhèn)江 湛江 宜賓 寶雞 黃南 南陽
巨人網(wǎng)絡通訊聲明:本文標題《PHP實用小技巧之調(diào)用錄像的方法》,本文關鍵詞 PHP,實用,小,技巧,之,調(diào)用,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。