可能的重复:
什么是PHP或Javascript中的Closures/Lambda外行术语?
'闭包'和'lambda'有什么区别?
嗨,
我一直无法找到一个明确解释闭包和匿名函数之间差异的定义.
我看到的大多数参考文献清楚地指出它们是不同的"事物",但我似乎无法理解为什么.
有人可以帮我简化一下吗?这两种语言功能之间有哪些具体差异?在哪些情况下哪一个更合适?
PHP 5.3提供匿名函数.
我应该使用它们还是避免使用它们?如果是这样,怎么样?
编辑 ; 刚刚用php匿名函数发现了一些不错的技巧......
$container = new DependencyInjectionContainer();
$container->mail = function($container) {};
$conteiner->db = function($container) {};
$container->memcache = function($container) {};
Run Code Online (Sandbox Code Playgroud) function convert($currencyType)
{
$that = $this;
return $result = function () use ($that)
{
if (!in_array($currencyType, $this->ratio))
return false;
return ($this->ratio[$currencyType] * $this->money); //a float number
};
}
$currency = new Currency();
echo $currency->convert('EURO');
Run Code Online (Sandbox Code Playgroud)
怎么了?
我收到错误消息:
Catchable fatal error: Object of class Closure could not be converted to string
Run Code Online (Sandbox Code Playgroud)