On 2012-04-13, Nicolas Grekas <nicolas.grekas+...@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            | matt...@zend.com
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to