Re: PATCH: anonymous functions in PHP
| From: | Michael Walter | Date: | Thu, 22 Mar 2007 14:54:36 +0000 |
| Subject: | Re: PATCH: anonymous functions in PHP | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-28551@lists.php.net to get a copy of this message | ||
On 3/22/07, Christian Schneider <cschneid@cschneid.com> wrote:
Michael Walter wrote: Which seems to fit Andi's $_SCOPE proposal. Yes, but is a point against a real closure proposal.I think the defining property of a "real" closure proposal is having the lexical scope available. So I guess we both agree on that something like $_SCOPE would be a useful thing to have.
Closures become really useful when you start building abstractions on top of anonymous functions, since in the process of abstraction you often replace literals/constants by parameters (similar to how function parameters become useful when you start refactoring common code into functions ;). A simple real-life example is: function pick($member) { return create_function('$x', 'return $x->'.$member.';'); } which could be used e.g. in array_map(pick('age'), $people). Arguably, in this case you could as well write: array_map(function($p) {return $p->age;}, $people); but I believe it is easy to see how the problem generalizes. Regards, MichaelIs that like "enterprise" examples? ;) No, it's like non-textbook example (which can also run on a Bird of Prey or a Klingon starship :-))He said real-life examples (-:C