php闭包使用例子
Posted 与f
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
关联文章:http://www.cnblogs.com/fps2tao/p/8727482.html
转:https://www.cnblogs.com/itfenqing/p/7073307.html
以上是关于php闭包使用例子的主要内容,如果未能解决你的问题,请参考以下文章
trait,interface,abstract,PHP7新特性以及PHP闭包学习
php的闭包(Closure)也就是匿名函数。是PHP5.3引入的。