PHP 中的 lambda 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1909002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What use is lambda in PHP?
提问by user151841
The lambda anonymous function is part of PHP 5.3. What use does it have? Is there anything that one can only do with lambda? Is lambda better for certain tasks?
lambda 匿名函数是 PHP 5.3 的一部分。它有什么用?有什么只能用 lambda 来做的吗?lambda 对某些任务更好吗?
I've seen the Fibonacci example, and I really don't need to write Fibonacci sequences, so I'm still not sure if it's that useful for the kinds of tasks I encounter in writing webbish applications. So what does one do with it in "real life"?
我看过斐波那契的例子,我真的不需要写斐波那契数列,所以我仍然不确定它对我在编写 webbish 应用程序时遇到的那种任务是否有用。那么,在“现实生活”中,人们用它做什么呢?
回答by Matt
Anything that requires a temporary function that you probably will only use once.
任何需要临时功能的东西,您可能只会使用一次。
I would use them for callbacks, for functions such as:
我会将它们用于回调,例如:
E.g.
例如
usort($myArray, function ($a, $b) {
return $a < $b;
});
Before 5.3, you'd have to..
在 5.3 之前,您必须...
function mySort ($a, $b) {
return $a < $b;
}
usort($myArray, 'mySort');
Or create_function ...
或 create_function ...
usort($myArray, create_function('$a, $b', 'return $a < $b;'));
回答by GZipp
Anonymous functions (closures) can be created as local functions (thus not pollluting the global space, as Dathan suggested).
匿名函数(闭包)可以创建为局部函数(因此不会污染全局空间,正如 Dathan 建议的那样)。
With the "use" keyword, variables that are passed to or created by the enclosing function can be used inside the closure. This is very useful in callback functions that are limited in their parameter list. The "use" variables can be defined outside the closure, eliminating the need to redefine them each time the closure is called.
使用“use”关键字,传递给封闭函数或由封闭函数创建的变量可以在闭包内使用。这在参数列表受限的回调函数中非常有用。“use”变量可以在闭包之外定义,从而无需在每次调用闭包时重新定义它们。
function change_array($arr, $pdo)
{
$keys = array('a', 'c');
$anon_func = function(& $val, $key) use ($keys, $pdo)
{
if (in_array($key, $keys) {
$pdo->query('some query using $key');
$val = $pdo->fetch();
}
}
arr_walk($arr, $anon_func);
return $arr;
}
$pdo = new($dsn, $uname, $pword);
$sample = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$sample = change_array($sample, $pdo);
(Of course, this example can be simpler without a closure, but it's just for demo.)
(当然,这个例子没有闭包可以更简单,但这只是为了演示。)
回答by 7hi4g0
Despite all of the uses one can think for lambda functions, in PHP it also allows something very special called closure. That is the ability to make variables in the current scope available to the function long after the current scope stops existing.
尽管人们可以想到 lambda 函数的所有用途,但在 PHP 中它还允许一种非常特殊的东西,称为闭包。这是在当前作用域停止存在后很长时间使当前作用域中的变量对函数可用的能力。
Just to mention some useful patterns that closure allow you, one can implement Memoization (Caching)and Curry.
仅提及闭包允许您使用的一些有用模式,可以实现Memoization(缓存)和Curry。
Also very useful are the throw-awayor callback functions that @Matt highlighted in his answer.
@Matt 在他的回答中突出显示的丢弃或回调函数也非常有用。
For more on closures, check this question: How do JavaScript closures work?
有关闭包的更多信息,请查看以下问题:JavaScript 闭包如何工作?
回答by Dathan
There are some use cases that are made more convenient by lambdas. For instance, if a method uses a callback, using a lambda for that callback means you don't have to define a "real" function somewhere to handle it. So, lambdas keep your code cleaner.
有一些用例通过 lambdas 变得更加方便。例如,如果一个方法使用回调,则为该回调使用 lambda 意味着您不必在某处定义“真实”函数来处理它。所以,lambdas 让你的代码更简洁。
As with the above, lambdas can be used as "short-lived" functions. For instance, sorting algorithms typically only need to know if variable ais less than variable bfor any combination of aand b. To make a generic sorting algorithm that's capable of handling any class of objects, you might make your sort function definition such that it accepts a function to use as a comparator. Providing a lambda as the comparator function means that you can define your sort behavior on a per-call basis, rather than having to define a "real" function that will live for the lifetime of your script just to handle this one sorting case.
与上面一样,lambdas 可以用作“短期”函数。例如,排序算法通常仅需要知道,如果变量a小于变量b对的任何组合a和b。要创建能够处理任何类对象的通用排序算法,您可以定义排序函数,使其接受一个函数作为比较器。提供 lambda 作为比较器函数意味着您可以在每次调用的基础上定义排序行为,而不必定义一个“真正的”函数,该函数将在脚本的整个生命周期内都存在,只是为了处理这种排序情况。
回答by stormist
The implementation of the cryptic Y combinator?
神秘的 Y 组合器的实现?
function Y($F)
{
$func = function ($f) { return $f($f); };
return $func(function ($f) use($F)
{
return $F(function ($x) use($f)
{
$ff = $f($f);
return $ff($x);
});
});
}
Cited Source: http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures
引用来源:http: //fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures

![php 给定的邮箱地址 [] 不符合 RFC 2822, 3.6.2。当电子邮件在变量中时](/res/img/loading.gif)