首页 > 代码库 > php闭包使用例子

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);

  

 

php闭包使用例子