$app->get('/', function() use ($app) {
$request = $app['request'];
});
The use keyword is good for importing anything from the current context into the closure. However, I'm not sure it's a good idea in this case because it seems unnecessary - which makes the example a bit of a straw man: The $app object already knows about itself, so why not do the simpler and more loosely bound version: $app->get('/', function($app) {
$request = $app['request'];
});
then when calling the closure, the object just has to do this: $closure($this);
That way you don't have to remember to "use ($app)" every time you define a closure (and I'm also not wild about overlapping variable names from different contexts).Now we have the new bindTo() method which results in yet more extra code:
$boundClosure = $closure->bindTo($context);
Granted, you could define a general bindTo mechanism once per class implementation, and being able to use $this inside the closure is nice, but I'm not sure this justifies an addition to the language's complexity when a perfectly fine method has been around that does the same thing (and with less code).