First excuse my english I'm not a native speaker and sorry if it looks rough, this is the first time that I post on this site. My problem is quite simple I think. Let's say, we have :
class A {
function foo() {
function bar ($arg){
echo $this->baz, $arg;
}
bar("world !");
}
protected $baz = "Hello ";
}
$qux = new A;
$qux->foo();
In this example, "$this" obviously doesn't refer to my object "$qux".
How should I do to make it reffer to "$qux"?
As might be in JavaScript : bar.bind(this, "world !")
PHP doesn't have nested functions, so in your example
bar
is essentially global. You can achieve what you want by using closures (=anonymous functions), which support binding as of PHP 5.4:UPD: however,
bindTo($this)
doesn't make much sense, because closures automatically inheritthis
from the context (again, in 5.4). So your example can be simply:UPD2: for php 5.3- this seems to be only possible with an ugly hack like this:
Here
get_object_vars()
is used to "publish" protected/private properties to make them accessible within the closure.