我一直在研究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在命名空间中使用的关键字,但在这里它看起来不同.
谢谢.