相关疑难解决方法(0)

在函数中使用关键字 - PHP

可能重复:
在Php 5.3.0中,功能"使用"标识符是什么?一个理智的程序员应该使用它吗?

我一直在研究PHP中的闭包,这引起了我的注意:

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

        $callback =
            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)

有人请给我一个关于use此代码中使用情况的解释.

function ($quantity, $product) use ($tax, &$total)
Run Code Online (Sandbox Code Playgroud)

当我use在PHP中搜索时,它会找到use在命名空间中使用的关键字,但在这里它看起来不同.

谢谢.

php closures anonymous-function

72
推荐指数
2
解决办法
5万
查看次数

标签 统计

anonymous-function ×1

closures ×1

php ×1