"致命错误:在不在对象上下文中时使用$ this"但在对象上下文中使用代码IS

Chr*_*son 0 php oop

我得到了旧的熟悉的"致命错误:在不在对象上下文中时使用$ this" $this->test = 'test';在下面的类中引用:

class Example {
    public $test;
    public function index() {
        $this->test = 'test';
    }
}
Run Code Online (Sandbox Code Playgroud)

类方法通过调用call_user_func_array(array('example', 'index'), $params);.我只能假设call_user_func_array由于某种原因决定将索引方法称为静态例如example::index()?但是我还没有想到解决这个问题,奇怪的是我直到最近才遇到问题.

Lin*_*een 5

这有效:

$obj = new Example();
call_user_func_array(array($obj, 'index'), $params);
Run Code Online (Sandbox Code Playgroud)

你的代码基本上做了:

Example::index($params);
Run Code Online (Sandbox Code Playgroud)

index静态调用,你正确地假设.