PHP闭包函数传参及使用外部变量的方法,php变量
PHP闭包函数传参及使用外部变量的方法,php变量
本文实例讲述了PHP闭包函数传参及使用外部变量的方法。分享给大家供大家参考,具体如下:
在Laravel控制器写两个方法,一个是在内部创建一个闭包函数,一个是执行传过来的闭包函数,测试闭包的写法,use使用外部变量,及闭包函数的传参。如下:
//测试闭包传参及use使用外部变量 public function testClosure($t1, $t2) { $closure = function ($param1, $param2) use ($t1, $t2) { echo $param1.$param2.$t1.$t2; }; $this->execClosure('test.closure', $closure); } //执行闭包函数 protected function execClosure($name, Closure $closure) { echo 'Closure func name:'.$name; echo '<br>'; $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
转自:小谈博客 http://www.tantengvip.com/2016/03/php-closure-use/
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
- PHP闭包函数详解
- php的闭包(Closure)匿名函数初探
- php的闭包(Closure)匿名函数详解
- PHP中的闭包(匿名函数)浅析
- PHP闭包实例解析
- PHP闭包(Closure)使用详解
- PHP用反撇号执行外部命令
- PHP 执行系统外部命令 system() exec() passthru()
- 在PHP中执行系统外部命令
- PHP 中执行系统外部命令

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Pelayar Peperiksaan Selamat
Pelayar Peperiksaan Selamat ialah persekitaran pelayar selamat untuk mengambil peperiksaan dalam talian dengan selamat. Perisian ini menukar mana-mana komputer menjadi stesen kerja yang selamat. Ia mengawal akses kepada mana-mana utiliti dan menghalang pelajar daripada menggunakan sumber yang tidak dibenarkan.

EditPlus versi Cina retak
Saiz kecil, penyerlahan sintaks, tidak menyokong fungsi gesaan kod

SublimeText3 Linux versi baharu
SublimeText3 Linux versi terkini

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

PhpStorm versi Mac
Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).
