PHP小技巧 之 調用錄像

主要功能

把你實際的調用操做錄下來,而後在你想要的地方從新調用
和匿名函數的做用基本同樣,暫存你的調用操做 通常用於鏈式調用, 而後實際做用於你想要操做的對象上面
好像和沒說同樣php

使用場景

假如 laravel 項目用到了 倉庫模式, 而後對於比較複雜的查詢條件,通常狀況下有三種操做laravel

  1. 針對特殊查詢增長方法
  2. 定一個規則,按照這個規則組裝數組,而後須要在 倉庫類 裏面實現解析
  3. 傳匿名函數,匿名函數裏面寫查詢條件

如今能夠對第三種方法進行優化,傳入一個下面代碼裏的 CallEcho 對象git

//控制器裏
$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());
    }
}

看着和匿名函數差很少,可是 CallEcho 能夠被繼承來實現些接口,繼承後還能夠對查詢條件進行一些操做,好比過濾什麼的。用匿名函數的話,徹底就看調用方的良心了。github

最重要的是不傳對象傳函數叫什麼面對對象數組

上代碼

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);

PS

靈感來源於slim3 中間件 的實現測試

歡迎你們留言

相關文章
相關標籤/搜索