Lexical scope to appear in PHP?

By | | Programming

9

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.

Get Started with
Ruby on Rails

Github, Twitter and Hulu. All huge. All successful. All Rails.

Learn the web development framework of the moment with our newest book and course.

Learn Rails

Troels Knak-Nielsen

Troels has been crafting web applications, as a freelancer and while employed by companies of various sizes, since around the time of the IT-bubble burst. Nowadays, he's working on backend systems for a Danish ISP. In his spare time, he develops and maintains Konstrukt, a web application framework for PHP, and is the organizer of a monthly PHP-meetup in Copenhagen.

More Posts

{ 5 comments }

P. Aichpy April 17, 2009 at 6:05 am

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.

kyberfabrikken February 3, 2008 at 8:39 pm

Hi Marcus,

That would be:
http://del.icio.us/kyberfabrikken

lastcraft February 3, 2008 at 7:58 am

Hi Troels…

What is your delicious name? I suspect we may have quite a few bookmarks in common.

yours, Marcus

honeymonster December 31, 2007 at 2:14 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.

binjured December 25, 2007 at 12:07 am

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)

Comments on this entry are closed.

{ 4 trackbacks }