Lexical scope to appear in PHP?

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.

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • Pingback: » Blog Archive » Lexical scope to appear in PHP - SitePoint

  • binjured

    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)

  • Pingback: developercast.com » SitePoint PHP Blog: Lexical scope to appear in PHP?

  • honeymonster

    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.

  • http://www.lastcraft.com/ lastcraft

    Hi Troels…

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

    yours, Marcus

  • kyberfabrikken

    Hi Marcus,

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

  • Pingback: SitePoint PHP Blog: Keeping current with PHP | Development Blog With Code Updates : Developercast.com

  • Pingback: ArkInfotec.com » Blog Archive » Keeping Current With PHP

  • P. Aichpy

    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.