Re: [PATCH] [RFC] Closures and lambda functions in PHP
| From: | Alexander Wagner | Date: | Fri, 20 Jun 2008 16:57:26 +0000 |
| Subject: | Re: [PATCH] [RFC] Closures and lambda functions in PHP | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-38483@lists.php.net to get a copy of this message | ||
> I think allowing globals/lexicals to be passed by value doesn't make
> much sense, you could just use a regular parameter for that.
No, that is highly inconvenient when you're doing actual functional
programming with higher order functions and everything.
$i = 1;
$incrementor = lambda ($x) lexical ($i) { return $x + $i; }
..
$arr = array_map($incrementor,$arr);
How would you do this by passing in $i as a parameter without a closure?
And if you use references, this may break if $i is a loop variable (the
canonical example of closure-WTF).
> And that would bring another inconsistency to PHP, and would mean that PHP
> is discouraging the use of $_GLOBALS, that was created to discourage to use
> of 'global $var'.
All languages accumulate cruft as they evolve.
global behaves in a manner that is inconsistent with pretty much everything
else in PHP (referencing by default), and variables can be declared global in
the middle of a function, which makes no sense at all. It would be a mistake
to be consistent with something so badly broken.
By "long term replacement for global" I meant to imply that it would actually
be a good idea to remove the old global keyword from the language. Deprecate
it in PHP 6 and kill it in PHP 7, or maybe in PHP 7 and 8. Cruft gone.
$GLOBALS should be kept around because it is not a reference and allows
acutally unset()ing a global variable, for example.
Gesundheit
Wag
--
Da saß ich nun, führte Krieg und hatte Geburtstag.
- Joschka Fischer