PHP RFC: Closure::call

Introduction

PHP has had Closures since 5.3, and since 5.4 has had Closure::bind (static method) and Closure::bindTo (method) to allow creating new closures that have $this bound to a specific method. However, it has not been possible to bind at call-time, and you must instead create a temporary new closure, making calling bound to multiple objects cumbersome and inefficient (at least two statements are needed, and a new closure must be created and immediately disposed of for each).

Proposal

A new method is added to Closure, with the following signature:

mixed Closure::call(object $to[, mixed ...$parameters])

It calls the closure with the given parameters and returns the result, with $this bound to the given object $to, using the scope of the given object. Like the bind(To) methods, a static class cannot be bound (using →call will fail).