Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a lambda function and a closure (in PHP)?

Chapter 2 of "Magento PHP Developer's Guide" states:

Zend Framework 2 uses 100% object-oriented code and utilizes most of the new features of PHP 5.3, namely namespaces, late static binding, lambda functions and closures.

While the post What is the difference between a 'closure' and a 'lambda'? has some answers (such as, that a lambda is just an anonymous function, and that a closure is a function which can access variables not in its parameter list), seems to be specific to the Python programming language (with some mention of the Scheme programming language). For instance, according to the post, in Python, it seems, there can be closures which are not lambdas, and lambdas which are not closures.

However, I am interested in the PHP programming language, not Python. One of the answers below seems to point out that in PHP all closures are lambdas, which conflicts with what the post relating to Python states.

It seems to me, that these concepts vary in the particulars from language to language, and I am interested in PHP, hence this post.

All of this is confusing. While I would assume that lambda functions in general are just unnamed functions, the following Wikipedia article says more about closures:

http://en.wikipedia.org/wiki/Closure_%28computer_science%29

although has no examples in PHP.

like image 491
John Sonderson Avatar asked Nov 12 '13 14:11

John Sonderson


People also ask

What is the difference between a lambda and a closure?

It's as simple as this: lambda is a language construct, i.e. simply syntax for anonymous functions; a closure is a technique to implement it -- or any first-class functions, for that matter, named or anonymous.

Is a lambda function a closure?

Lambda functions may be implemented as closures, but they are not closures themselves. This really depends on the context in which you use your application and the environment. When you are creating a lambda function that uses non-local variables, it must be implemented as a closure.

What is the difference between lambda and function?

The primary difference between a lambda and a regular function is that the lambda function evaluates only a single expression and yields a function object. Consequently, we can name the result of the lambda function and use it in our program as we did in the previous example.

What is a closure PHP?

Basically a closure in PHP is a function that can be created without a specified name - an anonymous function. Here's a closure function created as the second parameter of array_walk() . By specifying the $v parameter as a reference one can modify each value in the original array through the closure function.


1 Answers

A closure is a lambda function in php that encapsulates variables so they can be used once their original references are out of scope.

A closure is a lambda function, but a lambda function is not a closure unless you specify the use keyword.

This is a much better answer: https://stackoverflow.com/a/220728/1152375

like image 98
gh123man Avatar answered Oct 10 '22 00:10

gh123man