3 ms·
I am aware of the gnarliness of the following BUT You could do something like: class Object { public function __call($method, $args) { if
by shaunxcode 15y ago
I am aware of the gnarliness of the following BUT You could do something like:
class Object {
public function __call($method, $args) {
if(isset($this->$method) && $this->$method instanceof \Closure) {
$this->$method->bindTo($this);
return call_user_func_array($this->$method, $args);
}
}
}
$context = new Object;
$context->foo = "Hello World";
$context->closure = function() {
echo $this->foo;
};
$context->closure();
Once traits are in the language (5.5?) that could just be mixed in.