我正在检查一些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)
作为匿名函数的一个例子.
有人知道吗?有文件吗?如果它被使用它看起来很邪恶?
我正在尝试将一些额外的参数传递给我的FormEvent Listener,它通常只需要一个参数,实际的事件本身.我尝试过这样的事情:
$builder->addEventListener(FormEvents::PRE_SET_DATA,
function($event, $extraData) {
//Do stuff
}
);
Run Code Online (Sandbox Code Playgroud)
但是,这会返回一个错误,指出它缺少此函数的第二个参数.我愿意接受任何建议!谢谢!