I mentioned briefly, in my last post, that Wez Furlong made a patch back in March, that allows a native syntax for creating anonymous functions. This could replace the heinous create_function.
Since then, I brought up Wez’ original patch again on the php-internals lists. This has prompted some hefty debate over the last week. The main argument against approving the patch, seems to be, that one would expect static scoping rules to apply to the anonymous function. After all, this is the case in similar languages, which support anonymous functions. One could only assume, that changing PHP to support this, would be a major undertaking.
One would be wrong, it seems.
Today, Christian Seiler posted a patch to allow lexical scope for anonymous functions. There are a few loose ends, but it appears to work in general. Instead of making all variables follow the static scoping rules, a new keyword (lexical) is introduced. It works similar to global, in that it must be explicitly declared, which variables are lexically scoped. This is how it looks in action:
function getAdder($x) {
return function ($y) {
lexical $x;
return $x + $y;
};
}
$add2 = getAdder(2);
$add2(8); // return 10
It’s probably a bit early to tell if this will find its way into the language. It’s still just a proposal and it would take some further work to get it right, but at least it appears to be technically possible. We’ll have to wait and see.
Otherwise, there’s just left to wish you all a merry Christmas.







[...] Lexical scope to appear in PHP – SitePointLexical scope to appear in PHPSitePoint, Australia – Dec 22, 2007Since then, I brought up Wez? original patch again on the php-internals lists. This has prompted some hefty debate over the last week. … [...]
December 24th, 2007 at 8:03 pm
This is another case of PHP being “dumb,” for lack of a better term. Why can’t it just know the correct scope of x? Introducing this would probably cause more confusion than anything, created by people trying to create a normal anonymous function without using this “lexical” keyword.
How about…
def f1(x): return lambda y: x + y f1(2)(8) # return 10(Brought to you by the United Alliance for a Python Forum on SitePoint)
December 25th, 2007 at 12:07 am
[...] this new post to the SitePoint PHP blog, Troels Knak-Nielsen talks about some of the advancements (and [...]
December 26th, 2007 at 2:21 am
Seems this is sort of a half-baked closure. The lexical scoping rules is at odds with the object orientation of PHP5. You cannot access/capture member variables the sorrounding class without assigning them to a local variable first. This will probably work because of the rather strange alias assignment; so that you can do $localvar = $membervar; before accessing $localvar from inside the “closure”.
Does this work for simple types as well, or will the closure simply operate on the captured values?
To some extent this is analogous to the “assign before return” requirement like “return $dummy=(some expression)”. It is still not very elegant, though.
December 31st, 2007 at 2:14 am
Hi Troels…
What is your delicious name? I suspect we may have quite a few bookmarks in common.
yours, Marcus
February 3rd, 2008 at 7:58 am
Hi Marcus,
That would be:
http://del.icio.us/kyberfabrikken
February 3rd, 2008 at 8:39 pm
[...] which – as the name implies – contains RFC’s for improvements of the language. I’ve rambled on about closures and lambdas before, but as you can see, there is now an accepted patch. Whether it’ll make it into 5.3 is [...]
July 17th, 2008 at 12:09 am
[...] which – as the name implies – contains RFC’s for improvements of the language. I’ve rambled on about closures and lambdas before, but as you can see, there is now an accepted patch. Whether it’ll make it into 5.3 is [...]
August 5th, 2008 at 6:29 pm
I posted a feature request last year for an optional lexical scoping keyword. Unfortunately it seems to be just collecting dust:
http://bugs.php.net/bug.php?id=46504
The lack of this feature and a pre-execution check for undeclared variables (like ‘use strict’ in Perl) are 2 things that make PHP truly suck right now.
April 17th, 2009 at 6:05 am