佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > PHP實(shí)用小技巧之調(diào)用錄像的方法

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

熱門(mén)標(biāo)簽:Mysql連接數(shù)設(shè)置 Linux服務(wù)器 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 電子圍欄 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 服務(wù)器配置

主要功能

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

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

好像和沒(méi)說(shuō)一樣

使用場(chǎng)景

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

  • 針對(duì)特殊查詢?cè)黾臃椒?/li>
  • 定一個(gè)規(guī)則,按照這個(gè)規(guī)則組裝數(shù)組,然后需要在 倉(cāng)庫(kù)類 里面實(shí)現(xiàn)解析
  • 傳匿名函數(shù),匿名函數(shù)里面寫(xiě)查詢條件

現(xiàn)在可以對(duì)第三種方法進(jìn)行優(yōu)化,傳入一個(gè)下面代碼里的 CallEcho 對(duì)象

//控制器里
$callEcho = (new CallEcho())->where("username", "馬云")->where("is_boss", 1)->first();
$fubao = (new UserRepository)->first($callEcho);

//倉(cāng)庫(kù)類
class UserRepository{
  public function first(CallEcho $callEcho){
    return $callEcho->invoke(new User());
  }
}

看著和匿名函數(shù)差不多,但是 CallEcho 可以被繼承來(lái)實(shí)現(xiàn)些接口,繼承后還可以對(duì)查詢條件進(jìn)行一些操作,比如過(guò)濾什么的。用匿名函數(shù)的話,完全就看調(diào)用方的良心了。

最重要的是不傳對(duì)象傳函數(shù)叫什么面對(duì)對(duì)象

上代碼

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;
  }
  
  //作用于真正的對(duì)象上面
  public function invoke($obj){
    return call_user_func_array($this->callable, [$obj]);
  }
}

簡(jiǎn)單的測(cè)試 以及 使用

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)開(kāi)始****************/
/** @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

靈感來(lái)源于slim3 中間件 的實(shí)現(xiàn)

總結(jié)

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

您可能感興趣的文章:
  • 分享五個(gè)PHP7性能優(yōu)化提升技巧
  • PHP后門(mén)隱藏的一些技巧總結(jié)
  • php提高腳本性能的4個(gè)技巧
  • php把文件設(shè)置為插件的技巧方法
  • PHP學(xué)習(xí)的技巧和學(xué)習(xí)的要素總結(jié)
  • php探針使用原理和技巧講解
  • php和vue配合使用技巧和方法
  • php技巧小結(jié)【推薦】
  • 淺談PHP7中的一些小技巧

標(biāo)簽:蚌埠 棗莊 廣元 萍鄉(xiāng) 大理 衢州 衡水 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)用小技巧之調(diào)用錄像的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    怀化市| 嘉义县| 屏东县| 若羌县| 高要市| 类乌齐县| 竹溪县| 松潘县| 徐州市| 江孜县| 仲巴县| 永登县| 望谟县| 黎平县| 石门县| 双辽市| 宁阳县| 沂源县| 枣强县| 那坡县| 阿图什市| 门头沟区| 南江县| 历史| 安图县| 秀山| 武山县| 阿尔山市| 于都县| 建昌县| 通许县| 峨边| 元江| 原阳县| 弥渡县| 霞浦县| 拉萨市| 山西省| 扎赉特旗| 昭通市| 安宁市|