PHP闭包函数传参及使用外部变量的方法
网络编程 2021-07-05 09:10www.168986.cn编程入门
这篇文章主要介绍了PHP闭包函数传参及使用外部变量的方法,基于Laravel框架分析了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']);
访问.example./test/closure/hehe1/hehe2
浏览器输出结果:
Closure func name:test.closure p1p2hehe1hehe2
转自:小谈博客 http://.tanteng./2016/03/php-closure-use/
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》
希望本文所述对大家PHP程序设计有所帮助。
上一篇:php图片添加文字水印实现代码
下一篇:Yii rules常用规则示例
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程