|
|
|
|
|
by Udo
5357 days ago
|
|
It's definitely nice that closures are real objects, but I don't see the justification for the bindTo() method. Take this, for example (from the article): $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). |
|
The new array syntax, closures, binding ... I'm surprised you're not seeing the pattern. All of these features are borrowed from JavaScript. Absolutely fine by me. I already spend most of my time in JavaScript anyway. I'm willing to bet I'm not alone, hence the changes.