PHP實(shí)用小技巧之調(diào)用錄像的方法
主要功能
把你實(shí)際的調(diào)用操作錄下來,然后在你想要的地方重新調(diào)用
和匿名函數(shù)的作用基本一樣,暫存你的調(diào)用操作 一般用于鏈?zhǔn)秸{(diào)用, 然后實(shí)際作用于你想要操作的對象上面
好像和沒說一樣
使用場景
假如 laravel 項目用到了 倉庫模式, 然后對于比較復(fù)雜的查詢條件,一般情況下有三種操作
- 針對特殊查詢增加方法
- 定一個規(guī)則,按照這個規(guī)則組裝數(shù)組,然后需要在 倉庫類 里面實(shí)現(xiàn)解析
- 傳匿名函數(shù),匿名函數(shù)里面寫查詢條件
現(xiàn)在可以對第三種方法進(jì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 可以被繼承來實(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é)束****************/
//基本上和這個作用一樣
$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í)價值,謝謝大家對我們的支持。
上一篇:Laravel框架中的路由和控制器操作實(shí)例分析
欄 目:PHP編程
下一篇:PHP設(shè)計模式之工廠模式(Factory)入門與應(yīng)用詳解
本文標(biāo)題:PHP實(shí)用小技巧之調(diào)用錄像的方法
本文地址:http://www.jygsgssxh.com/a1/PHPbiancheng/11054.html
您可能感興趣的文章
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂碼
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)語句
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)據(jù)函數(shù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方案
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文件的語句


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什


