php的闭包(Closure)也就是匿名函数。是PHP5.3引入的。
闭包的语法很简单,需要注意的关键字就只有use,use意思是连接闭包和外界变量。
$a = function() use($b) {
}
闭包的作用
01、依据闭包实现一个容器
class Di
{
private $factory;
public function set($id, $value)
{
$this->factory[$id] = $value;
}
public function get($id)
{
$val = $this->factory[$id];
return $val();//如果不加括号,仅仅返回的是闭包类,并不是User实例
}
}
class User
{
private $username;
public function __construct($username = '')
{
$this->username = $username;
}
public function getUserName()
{
return $this->username;
}
}
$di = new Di();
// 在此使用了闭包,所以实际上并不会实例化User类,只有在后面get的时候才会实例化
$di->set('a', function(){
return new User('张三');
});
var_dump($di->get('a')->getUserName());
02、使用闭包作为回调(减少 foreach 循环次数)
class Cart
{
CONST PRICE_BUTTER = 1.0;
CONST PRICE_MILK = 5.05;
protected $products = [];
public function add($product, $quantity)
{
$this->products[$product] = $quantity;
}
public function getQuantity($product)
{
return isset($this->products[$product]) ? $this->products[$product]: false;
}
public function getTotal($tax)
{
$total = 0.00;
$callback = function($quantity, $product) use ($tax, &$total) {
$priceItem = constant(__CLASS__ . '::PRICE_' . strtoupper($product));
$total += ($priceItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback); //参考array_walk 函数
return round($total, 2);
}
}
$cart = new Cart();
$cart->add('butter', 1);
$cart->add('milk', 5);
echo $cart->getTotal(0.05);
03、减少函数的参数
function html ($code , $id="", $class=""){
if ($id !== "") $id = " id = \"$id\"" ;
$class = ($class !== "")? " class =\"$class\"":">";
$open = "<$code$id$class";
$close = "</$code>";
return function ($inner = "") use ($open, $close){
return "$open$inner$close";
};
}
如果是使用平时的方法,我们会把inner放到html函数参数中,这样不管是代码阅读还是使用都不如使用闭包
$html = html("div","",'');
var_dump($html);
查看源代码才能查到最后一个参数的值
object(Closure)#1 (2) { ["static"]=> array(2) { ["open"]=> string(5) "<div>" ["close"]=> string(6) "</div>" } ["parameter"]=> array(1) { ["$inner"]=> string(10) "<optional>" } }
// 本身的东西 以及一个参数
echo $html();
闭包里面是 echo 这里就可以省略echo
04、解除递归函数
<?php
$fib = function($n) use(&$fib) {
if($n == 0 || $n == 1) return 1;
return $fib($n - 1) + $fib($n - 2);
};
echo $fib(2) . "\n"; // 2
$lie = $fib;
$fib = function(){die('error');};// 重新定义fib变量
echo $lie(5); // error 引用了上面代码
05、关于延迟绑定
<?php
$result = 0;
$one = function(){
var_dump($result);
};
$two = function() use ($result){
var_dump($result);
};
$three = function() use (&$result){
var_dump($result);
};
$result++;
$one(); // 匿名函数不引入参数 相当于局部的参数
$two(); // 匿名函数定义的时候就已经引入参数,后面参数变化无关
$three(); // 引用参数得以实现延迟加载
如果你需要延迟绑定use里面的变量,你就需要使用引用
06、使用闭包函数调用类中方法
class Grid
{
protected $builder;
protected $attribute;
public function __construct(Closure $builler)
{
$this->builder = $builler;
}
public function addColumn($name, $value)
{
$this->attribute[$name] = $value;
return $this;
}
public function build()
{
// 这儿回调闭包函数,参数为this
call_user_func($this->builder, $this);// 第一个参数是闭包函数 第二个是本类 this
}
public function __toString() // 类中实现了tostring 就可以被echo 出来
{
$this->build();
$str = '';
$call = function($val, $key) use(&$str) {
$str .= "$key=>$val;";
};
array_walk($this->attribute, $call);
return $str;
}
}
$grid = new Grid(
// 传入闭包函数,带参数
function($grid) {
$grid->addColumn('key1', 'val1');
$grid->addColumn('key2', 'val2');
}
);
echo $grid;