考虑以下代码,即使我使用Using $this when not in object context
函数设置bindTo
Class A
class A {
protected $name = 'a';
function _do(Closure $something) {
$var = function()use ($something) {
// suppose to bind the instance of class a for $something
$something->bindTo($this);
$something();
};
$var();
}
}
$a = new A();
$a->_do(function() {
// using $this as an instance of class A
echo $this->name;
});
答案 0 :(得分:1)
bindTo
返回一个新的匿名函数。它不会改变现有的。