PHP中使用閉包函數(shù)傳參,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
在Laravel控制器寫兩個方法,一個是在內部創(chuàng)建一個閉包函數(shù),一個是執(zhí)行傳過來的閉包函數(shù),測試閉包的寫法,use使用外部變量,及閉包函數(shù)的傳參。如下:
//測試閉包傳參及use使用外部變量 public function testClosure($t1, $t2) { $closure = function ($param1, $param2) use ($t1, $t2) { echo $param1.$param2.$t1.$t2; }; $this->execClosure('test.closure', $closure); } //執(zhí)行閉包函數(shù) protected function execClosure($name, Closure $closure) { echo 'Closure func name:'.$name; echo '
'; $closure('p1', 'p2'); }
在routes.php添加路由:
復制代碼 代碼如下:
Route::get('/test/closure/{t1}/{t2}',['uses'=>'TestController@testClosure']);
訪問www.example.com/test/closure/hehe1/hehe2
瀏覽器輸出結果:
Closure func name:test.closure p1p2hehe1hehe2
關于PHP中使用閉包函數(shù)傳參問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。