欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實(shí)用小技巧之調(diào)用錄像的方法

 更新時(shí)間:2019年12月05日 08:35:50   作者:kyanag  
這篇文章主要給大家介紹了關(guān)于PHP實(shí)用小技巧之調(diào)用錄像的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者實(shí)用PHP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

主要功能

把你實(shí)際的調(diào)用操作錄下來,然后在你想要的地方重新調(diào)用

和匿名函數(shù)的作用基本一樣,暫存你的調(diào)用操作 一般用于鏈?zhǔn)秸{(diào)用, 然后實(shí)際作用于你想要操作的對象上面

好像和沒說一樣

使用場景

假如 laravel 項(xiàng)目用到了 倉庫模式, 然后對于比較復(fù)雜的查詢條件,一般情況下有三種操作

  • 針對特殊查詢增加方法
  • 定一個(gè)規(guī)則,按照這個(gè)規(guī)則組裝數(shù)組,然后需要在 倉庫類 里面實(shí)現(xiàn)解析
  • 傳匿名函數(shù),匿名函數(shù)里面寫查詢條件

現(xiàn)在可以對第三種方法進(jìn)行優(yōu)化,傳入一個(gè)下面代碼里的 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 可以被繼承來實(shí)現(xiàn)些接口,繼承后還可以對查詢條件進(jì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();

/*************重點(diǎn)開始****************/
/** @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());
/************重點(diǎn)結(jié)束****************/

//基本上和這個(gè)作用一樣
$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 中間件 的實(shí)現(xiàn)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論