Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration
| From: | Matthew Weier O'Phinney | Date: | Fri, 13 Apr 2012 18:32:02 +0000 |
| Subject: | Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-59893@lists.php.net to get a copy of this message | ||
On 2012-04-13, Nicolas Grekas <nicolas.grekas+php@gmail.com> wrote:
> --f46d04016a77a386cf04bd8a62df
> Content-Type: text/plain; charset=ISO-8859-1
>
> > $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.
You're missing the point.
Yes, the original used "$this->getFooBar()" -- but it could have been
"$someOtherObject->getFooBar()" -- in which case you either have to bind
$someOtherObject, or grab the return of getFooBar().
Also, getFooBar() would be called _once_ in the original example,
instead of once each time the closure is invoked (your example).
> > 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?
The closure is a function itself; readability within it is just as
important as readability of the parent. I'd personally rather not have a
lot of boilerplate code marshalling variables to start off my closure --
I'd rather they were simply declared and ready for use.
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc