bobthecow/mustache.php — pull request #125 by bobthecow (2012-11-28 20:12:09+00:00)
Title: Bind section lambdas?

In PHP 5.4, we can re-bind Closures, essentially giving them a new `$this` internally. This change does that for higher order sections (section lambdas). Which means we can use this:

```php
'win',
'embiggen' => function($text) {
return strtoupper($this->render($text));
},
);
```

instead of this:

```php
'win',
'embiggen' => function($text, $mustache) {
return strtoupper($mustache->render($text));
},
);
```

My question is: is this a good thing? Should it be optional? Should it even be offered?

Pros:

* It saves a bit of typing.
* `$this->render` seems like a natural fit if you think of the Closure as the context of the section.
* `$this` could potentially expose other things to the section lambda in the future, such as the current indent level of the template.

Cons:

* (if always-enabled) it prevents more advanced Closure usage, for example, having a ViewModel object bind the closures it returns to itself, giving them access to private or protected members.
* (if configurable) it presents an interesting — and possibly hard to track down — situation when the engine instantiation changes. Trying to figure out why `$this` isn't what you expect it to be is sometimes really confusing, c.f. JavaScript :)
* (if configurable) it is yet another configuration option, but one that's only available for some versions of PHP.
* Other future things (indent level, etc) could also be exposed via the `$mustache` argument passed to the Closure.

Neutral:

* Because it's compiled-in, it doesn't have a measurable effect on performance either way.
* This shortcut is not available for non-Closure callables, e.g. the `array($this, 'doFoo')` style callables.

See #101

Please discuss.