php闭包使用例子
一.依据闭包实现一个容器
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());
二.使用闭包作为回调
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); return round($total, 2); } } $cart = new Cart(); $cart->add('butter', 1); $cart->add('milk', 5); echo $cart->getTotal(0.05);
三.使用闭包函数调用类中方法
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); } public function __toString() { $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;
闭包在对象中的一个典型使用场景就是留出一个方法给调用者,让调用者自主决定或组装数据.
如下例:让调用者决定如何组装或显示info.
class CalledClass { private $name; private $age; private $info; public function __construct($name, $age, Closure $callable) { $this->name = $name; $this->age = $age; call_user_func($callable, $this); // 这相当于 // $callable($this); } public function setInfo($info) { $this->info = $info; } public function getInfo() { echo $this->info; } /** * @return mixed */ public function getName() { return $this->name; } /** * @return mixed */ public function getAge() { return $this->age; } } $c = new CalledClass('php', 10, function (CalledClass $c) { echo $c->getAge() . $c->getName(); $c->setInfo($c->getAge() . $c->getName()); }); echo $c->getInfo();