码迷,mamicode.com
首页 > Web开发 > 详细

php闭包使用例子

时间:2018-04-16 16:09:37      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:class   htm   闭包   walk   UI   his   实现   code   set   

一.依据闭包实现一个容器

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闭包使用例子

标签:class   htm   闭包   walk   UI   his   实现   code   set   

原文地址:https://www.cnblogs.com/fps2tao/p/8855945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!