Nicolas >> $closure = function () use ($this->getFooBar() as $foo) { >> $foo->stuff(); >> } > > But this can already be written as : > > $closure = function () { > $foo = $this->getFooBar(); > $foo->stuff(); > }
Except that's not equivilant. getFooBar() in the first example is called exactly once, at creation time. getFooBar in the second example is called multiple times (once per invocation of $closure()). A minor difference, with huge consequences. Both in terms of performance (imagine using that callback in a usort function against a large array), and state (what if the value changes over time, the closure will change "magically" (which may be wanted, but I don't think so since it was bound in use)... It's worth pointing out, but your example is not the same. The only thing you can do now, is call the method outside the closure: $foo = $this->getFooBar(); $closure = function() use ($foo) { $foo->stuff(); } Anthony > 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php