相关疑难解决方法(0)

在PHP中,什么是闭包,为什么它使用"use"标识符?

我正在检查一些PHP 5.3.0功能,并在网站上遇到一些看起来很有趣的代码:

public function getTotal($tax)
{
    $total = 0.00;

    $callback =
        /* This line here: */
        function ($quantity, $product) use ($tax, &$total)
        {
            $pricePerItem = constant(__CLASS__ . "::PRICE_" .
                strtoupper($product));
            $total += ($pricePerItem * $quantity) * ($tax + 1.0);
        };

    array_walk($this->products, $callback);
    return round($total, 2);
}
Run Code Online (Sandbox Code Playgroud)

作为匿名函数的一个例子.

有人知道吗?有文件吗?如果它被使用它看起来很邪恶?

php closures

387
推荐指数
5
解决办法
17万
查看次数

标签 统计

closures ×1

php ×1