<a href="#106912" class="name"> <strong class="user"><em>amica at php-resource dot de</em></strong></a><a class="genanchor" href="#106912"> ¶</a>
有了可重新绑定的 $this,就可以做一些邪恶的事情
<span class="default"><?php
class A {
private $a = 12;
private function getA () {
return $this->a;
}
}
class B {
private $b = 34;
private function getB () {
return $this->b;
}
}
$a = new A();
$b = new B();
$c = function () {
if (property_exists($this, "a") && method_exists($this, "getA")) {
$this->a++;
return $this->getA();
}
if (property_exists($this, "b") && method_exists($this, "getB")) {
$this->b++;
return $this->getB();
}
};
$ca = $c->bindTo($a, $a);
$cb = $c->bindTo($b, $b);
echo $ca(), "\n"; echo $cb(), "\n"; ?>