PHP 闭包详解及实例代码
闭包和匿名函数在PHP5.3.0中引入的。
闭包是指:创建时封装周围状态的函数。即使闭包所处的环境不存在了,闭包中封装的状态依然存在。
理论上,闭包和匿名函数是不同的概念。但是PHP将其视作相同概念。
实际上,闭包和匿名函数是伪装成函数的对象。他们是Closure类的实例。
闭包和字符串、整数一样,是一等值类型。
创建闭包
<?php
$clousre=function($name){
return'Hello'.$name;
};
echo$closure('nesfo');
我们之所以能调用$closure变量,是因为这个变量的值是一个闭包,而且闭包对象实现了__invoke()魔术方法。只要变量名后有(),PHP就会查找并调用__invoke()方法。通常会把PHP闭包当作函数的回调使用。array_map(),preg_replace_callback()方法都会用到回调函数,这是使用闭包的最佳时机!
举个例子:
<?php
$numbersPlusOne=array_map(function($number){
return$number+1;
},[1,2,3]);
print_r($numbersPlusOne);
得到结果:
[2,3,4]
在闭包出现之前,只能单独创建具名函数,然后使用名称引用那个函数。这么做,代码执行会稍微慢点,而且把回调的实现和使用场景隔离了。
<?php
functionincrementNum($number){
return$number+1;
}
$numbersPlusOne=array_map('incrementNum',[1,2,3]);
print_r($numbersPlusOne);
附加状态
匿名函数不止可以当回调使用,还可以为PHP附加并封装状态。
PHP中,必须手动调用闭包对象的bindTo()方法或者使用use关键字,才能把状态附加到PHP闭包上。
<?php
functionenclosePerson($name){
returnfunction($doCommand)use($name){
return$name.','.$doCommand;
}
}
$clay=enclosePerson('Clay');
echo$clay('getmesweettea!');
得到结果:
"Clay,getmesweettea!"
PHP闭包是对象,每个闭包实例都可以使用$this关键字获取闭包的内部状态。闭包对象的默认状态没什么用,只有__invoke()方法和bindTo方法而已。
我们可以使用bindTo()这个方法,将Closure对象的内部状态绑定到其它对象上。
bindTo()方法的第二个参数:其作用是指定绑定闭包的那个对象所属的PHP类。因此,闭包可以访问绑定闭包的对象中受保护和私有的成员。
PHP框架经常使用bindTo()方法把路由URL映射到匿名回调函数上。这么做可以在这个匿名函数中使用$this关键字引用重要的应用对象。
使用bindTo()方法附加闭包状态
<?php
classApp
{
protected$routes=[];
protected$responseStatus='200OK';
protected$responseContentType='text/html';
protected$responseBody='Helloworld';
publicfunctionaddRoute($routePath,$routeCallback){
$this->routes[$routePath]=$routeCallback->bindTo($this,__CLASS__);
}
publicfunctiondispatch($currentPath){
foreach($this->routesas$routePath=>$callback){
if($routePath===$currentPath){
$callback();
}
}
header('HTTP/1.1'.$this->responseStatus);
header('Content-type:'.$this->responseContentType);
header('Content-length'.mb_strlen($this->responseBody));
echo$this->responseBody;
}
}
<?php
$app=newApp();
$app->addRoute('/user/nesfo',function(){
$this->responseContentType='application/json;charset=utf8';
$this->responseBody='{"name":"nesfo"}';
});
$app->dispatch('/user/nesfo');
以上就是对PHP闭包资料的资料整理,后续继续补充相关资料谢谢大家对本站的支持!