Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration
| From: | Nicolas Grekas | Date: | Fri, 13 Apr 2012 07:25:43 +0000 |
| Subject: | Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-59856@lists.php.net to get a copy of this message | ||
> $closure = function () use ($this->getFooBar() as $foo) {
> $foo->stuff();
> }
But this can already be written as :
$closure = function () {
$foo = $this->getFooBar();
$foo->stuff();
}
Here, $foo also only exists inside the closure.
> Also, remember that the closure is in fact another function, a function
> that performs its own actions independent of the parent.
Actions may be independent, but as a closure is declared inside its parent,
both codes have very strong relationship. For the reader, overriding this
relationship with new closure-local var names can weaken its understanding
of the code don't you think?
> function () use($long as $l, &$long as $r) {
> }
is currently written as
function () use(&$long) {
$l = $long;
$r =& $long;
}
While it's a bit longer, the only difference is that $long doesn't exists
inside the closure with use.. as ...
But is it a strong enough benefit to introduce a new syntax? Considering as
I said above that the vars "used" inside closures should keep their parents
scope name for readability, I personally fail to think so currently. Did I
miss a other benefit?
regards,
Nicolas