Re: PATCH: anonymous functions in PHP

From: Date: Mon, 19 Mar 2007 18:30:49 +0000
Subject: Re: PATCH: anonymous functions in PHP
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-28431@lists.php.net to get a copy of this message
> Well, making it work makes this thing closure. Otherwise it's just a > nice way to save a couple of keystrokes :) Not to diminish your work, > but there's a danger people would think it is closure because it looks > like one (i.e., in other languages closures look exactly this way, e.g. > Javascript). I think this is the key to this whole discussion. I doubt PHP will ever be able to fully support JavaScript- and Lisp-style closures. To take Stas' example to another level: function foo() { $bar = function () { echo "bar"; } return $bar; } $bar = foo(); $bar(); // does this work? According to our current current syntax: function foo() { $bar = create_function('', 'echo "bar";'); return $bar; } $bar = foo(); $bar(); // this does work I don't know Lisp very well at all, but in JavaScript, functions are general containers, and this sort of thing works fine, albeit VERY differently. PHP's scoping rules are already completely different from JavaScript's (there's no way to access the parent scope unless it happens to be the global scope). I strongly prefer Wez's syntax for anonymous function declaration. It would help on many levels, from readability to syntax-highlighting, to optimization (maybe...). On optimization, the question becomes "how does Wez's proposal tokenize?" --------Old: T_OPEN_TAG : <?php T_VARIABLE : $bar T_WHITESPACE : = T_WHITESPACE : T_STRING : create_function ( T_CONSTANT_ENCAPSED_STRING : '' , T_WHITESPACE : T_CONSTANT_ENCAPSED_STRING : 'echo "bar";' ) ; --------New: T_OPEN_TAG : <?php T_VARIABLE : $bar T_WHITESPACE : = T_WHITESPACE : T_FUNCTION : function ( ) T_WHITESPACE : { T_WHITESPACE : T_ECHO : echo T_WHITESPACE : T_CONSTANT_ENCAPSED_STRING : "bar" ; T_WHITESPACE : } ; If the answer is "New", then this could be compiled at.. well, compile-time, not at execute time. That could be even more interesting. (sorry for the long post.. most of it's code (-; ) S

« previous php.internals (#28431) next »