Hello
Re this page:
http://php.net/manual/en/functions.anonymous.php
Outside the world of PHP nobody seem to be using the word closure as if
it was a synonym to anonymous function.
I will provide some examples:
<blockquote
cite="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Closures">
...MyFunc has BECOME a closure. A closure is a special kind of object
that combines two things: a function, and the environment in which that
function was created.
</blockquote>
<blockquote cite="http://www.javascriptkit.com/javatutors/closures.shtml">
Two one sentence summaries:
* a closure is the local variables for a function - kept alive
after the function has returned, or
* a closure is a stack-frame which is not deallocated when the
function returns. (as if a 'stack-frame' were malloc'ed instead of being
on the stack!)
<blockquote>
<blockquote cite="http://www.webreference.com/programming/javascript/rg36/">
A closure takes place when a function creates an environment that binds
local variables to it in such a way that they are kept alive after the
function has returned. A closure is a special kind of object that
combines two things: a function, and any local variables that were
in-scope at the time that the closure was created.
</blockquote>
(Re 2nd sentence: I don't know if this is the original or if MDC is.)
See also JavaScript, The Definitive Guide, by David Flanagan, (5th ed)
section 8.8.4 on page 143f:
"This combination of code and scope is known as a closure in the
computer science literature" (p.144 - the word this refers to inner
functions keeping outer functions data even after return as discussed
previously by DF.)
It has been my job for a few years to evaluate books on JavaScript and I
have read more than 10 published between 2005 and 2009. They are very
consistent in their usage of the word closure.
To top things off, it seems that I am in agreement with the editors on
Wikipedia:
<blockquote
cite="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">
The term closure is often mistakenly used to mean anonymous function.
This is probably because most languages implementing anonymous functions
allow them to form closures and programmers are usually introduced to
both concepts at the same time. These are, however, distinct concepts.
</blockquote>
Computer science savvy people, like Douglas Crockford, tend to call
anonymous functions lambda. Sometimes (C# et al) they are called "lambda
expressions". However, lambda might not be a 100 % fit either from a
computer science perspective, but it sure is a lot better fit than closure.
I think that the PHP community perhaps calls anonymous functions
closures because it is one word less to say and type, and we might need
a single word term to use.
Therefore I'd suggest a rewrite of the manual page.
First paragraph, suggested text:
"Anonymous functions, also known as lambda, allow the creation of
functions which have no specified name. They are most useful as the
value of callback parameters, but they have many other uses."
Second paragraph introduces a problem, since it would references a
misnamed internal class. I'd suggest that is made anonymous!
"Anonymous functions can also be used as the values of variables; PHP
automatically converts such expressions into instances of the
corresponding internal class. Assigning an anonymous function to a
variable uses the same syntax as any other assignment, including the
trailing semicolon:
Third paragraph, suggested text:
"Lambda functions may also inherit variables from the parent scope, in
which case they are called closures."
Fourth paragraph cannot be changed of course, until the internal Closure
class has been appropriately renamed.
--
Keryx Web (Lars Gunther)
http://keryx.se/
http://twitter.com/itpastorn/
http://itpastorn.blogspot.com/