PHP中闭包的使用场景有: 1、动态调用静态类时; 2.用在回调函数中; 3. 赋值给普通变量; 4、使用use从域中的父Inheritance中获取值; 5.传递参数等时
在回调函数中使用
并赋值给普通变量
使用use从父域继承
登录后复制
提交参数
登录后复制
OO中的使用
_factory[$id] = $value; } 公共函数 get($id){ $value = $this->_factory[$id]; 返回 $value(); } } 类用户{ 私人$_用户名; 函数 __construct($username="") { $this->_username = $用户名; } 函数 getUserName(){ 返回$this->_用户名; } } $factory = 新工厂(); $factory->set("zhangsan",function(){ return new User('张三'); }); $factory->set("lisi",function(){ return new User("李四"); }); echo $factory->get("zhangsan")->getUserName();echo $factory->get("lisi")->getUserName();
函数中的调用
登录后复制