Re: PATCH: anonymous functions in PHP

From: Date: Mon, 19 Mar 2007 21:55:00 +0000
Subject: Re: PATCH: anonymous functions in PHP
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-28459@lists.php.net to get a copy of this message
This gets incredibly complex not only to figure out what to do in the PHP source, but for scripters to figure out what the heck it does...
Welcome to the world of closures :) But in most cases people use it to do pretty basic stuff (unless they are CS majors with too much time on their hands ;) - like creating "dynamic anonymous functions" - basically resulting in the same as what create_function does, and some currying - which, for those who doesn't know it, is technique of partially instantiating a multi-argument function - i.e. making something like: $addtwo = function ($a) { return $a+2; } $six = $addtwo(4); or more frequently something like this: $var = "foo"; $compare_to_var = function($s) { return (str_compare($var, $s)<0); } $strings_less_than_var = array_filter($array, $compare_to_var); which is really powerful technique for dynamic language, though can easily lead one to lose all track of what is happening if overused. For more confusion see http://en.wikipedia.org/wiki/Currying :)
If it's making YOUR head spin, what will it do to the poor unwashed masses?...
Unwashed masses better not do it :) Actually many "washed" masses have hard time to understand these things too.
I suspect that you could get away with JUST using global and/or static as they exist now, not introduce yet another scoping keyword, and anything that *NEEDS* to be done with an anonymous function can be done.
I guess this depends on definition of "NEEDS".
You have to have a pretty esoteric function to need that kind of scope control, no?
Depends on your background. Some people consider LISP intuitive language :) And if you work with Javascript, you probably would be doing it daily - most AJAX toolkits rely on heavy usage of anonymous functions. Not sure it needs to be in PHP though. It might be cool thing, but it's definitely becomes very confusing very fast. -- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/

« previous php.internals (#28459) next »