我正在检查一些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或Javascript中的Closures/Lambda外行术语?
'闭包'和'lambda'有什么区别?
嗨,
我一直无法找到一个明确解释闭包和匿名函数之间差异的定义.
我看到的大多数参考文献清楚地指出它们是不同的"事物",但我似乎无法理解为什么.
有人可以帮我简化一下吗?这两种语言功能之间有哪些具体差异?在哪些情况下哪一个更合适?
您好我正在作为laravel初学者工作,我在中间看到一个功能,功能如下:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check())
{
return redirect('/home');
}
return $next($request);
}
Run Code Online (Sandbox Code Playgroud)
Closure是什么,它有什么作用?