最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php怎么用函数外的变量?PHP闭包函数传参及使用外部变量的方法
时间:2022-06-24 19:47:25 编辑:袖梨 来源:一聚教程网
在Laravel控制器写两个方法,一个是在内部创建一个闭包函数,一个是执行传过来的闭包函数,测试闭包的写法,use使用外部变量,及闭包函数的传参。如下:
| 代码如下 | 复制代码 |
//测试闭包传参及use使用外部变量 publicfunctiontestClosure($t1,$t2) { $closure=function($param1,$param2)use($t1,$t2) { echo$param1.$param2.$t1.$t2; }; $this->execClosure('test.closure',$closure); } //执行闭包函数 protectedfunctionexecClosure($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
相关文章
- 歪歪漫画app官方正版下载入口怎么进-歪歪漫画app最新版本安装 04-30
- 哔咔漫画在线入口在哪-2026最新官方网页版地址链接与网址分享 04-30
- 《最终幻想7 重生》续作开发“按计划进行中” 04-30
- 羞羞漫画登录页面-羞羞漫画网页登录 04-30
- 怎么在《Vampire Crawlers》中快速刷金币 04-30
- Deepseek官方下载-Deepseek国际版下载 04-30