【发布时间】:2011-09-13 07:31:05
【问题描述】:
可能重复:
In Php 5.3.0 what is the Function “Use” Identifier ? Should a sane programmer use it?
我一直在研究 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);
}
请给我解释一下use在这段代码中的用法。
function ($quantity, $product) use ($tax, &$total)
当我在 PHP 中搜索 use 时,它会找到 use 关键字,它在命名空间中使用,但在这里看起来不同。
谢谢。
【问题讨论】:
-
完全相同的问题here。
-
评论
/* use ($tax, &$total) */会回答这个问题。敢于探索:)
标签: php closures anonymous-function