I’ve searched and searched but it seems like its not possible to reference $this within a closure. I would like to achieve an interface similar to JQuery as shown below. It doesn’t seem like this possible.
This cannot be done using “use” to import parent-scoped this into context. And the use of reflection looks like an overkill to me. Why to? This seems to me artificial and you, probably, should seek another way.
You can NOT pass $this in use. You can reference $this as in Tom’s second example above. Be careful, the closure will only have access to the public properties of $this.
What, you mean like… ehr… foreach and array_map? Don’t mean to trump your thought, TomB, but I don’t see the benefit of this versus “normal” looping. I do see the downsides though.
Alright, so it can’t be done, yet - awesome. That pretty much confirms what I already knew. fyi the object reference can’t be brought into the closure scope because it isn’t available. The object being feed into the closure is available within the collection returned via find().
For anyone browsing the thread who hasn’t read the RFC above, or whose eyes glazed over when trying, here’s the current implementation in PHP’s trunk (meaning, download a snapshot now and it will be available but may change at any point). The key part being the Closure::bindTo() method.
<?php
class Example {
protected $message = 'hello world';
public function test(Closure $callback) {
$bound = $callback->bindTo($this);
$bound();
}
}
$ex = new Example;
$ex->test(function(){
var_dump($this->message);
});
That is interesting, closures will have access to private and protected members? Wasn’t expecting that, just public. Not sure if I like that a closure can access all class members regardless of being private or protected. In that sense a closure can be used as a virtual extension of an object… kinda neat.
It allows for late blending of methods, and is one way of emulating multiple inheritance in a manner similar to Ruby’s mixin approach. Basically you have a set of methods you’d like an object to have, but don’t necessarily want to tie it to the objects ancestry. One way to handle this is to have closures that you add to a method.