【发布时间】:2012-10-04 20:34:05
【问题描述】:
我有一个方法,简化如下:
class Foo {
public function bar($id) {
// do stuff using $this, error occurs here
}
}
这样称呼效果很好:
$foo = new Foo();
$foo->bar(1);
但是,如果我使用call_user_func_array() 调用它,像这样:
call_user_func_array(array("Foo", "bar"), array('id' => 1));
应该相等,我得到以下错误:
致命错误:在
中不在对象上下文中时使用 $this
($this 未定义)
这是为什么?有什么我想念的吗?我应该怎么做才能在被调用的方法中仍然可以使用$this?
【问题讨论】: