Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php &引用;致命错误:不在对象上下文中使用$this“;但代码是在对象上下文中_Php_Oop - Fatal编程技术网

Php &引用;致命错误:不在对象上下文中使用$this“;但代码是在对象上下文中

Php &引用;致命错误:不在对象上下文中使用$this“;但代码是在对象上下文中,php,oop,Php,Oop,我得到了熟悉的“致命错误:当不在对象上下文中时使用$this”,指的是$this->test='test'在以下类中: class Example { public $test; public function index() { $this->test = 'test'; } } 通过调用用户函数数组(数组('example','index'),$params)调用类方法。我只能假设call\u user\u func\u array出于某种原因

我得到了熟悉的“致命错误:当不在对象上下文中时使用$this”,指的是
$this->test='test'在以下类中:

class Example {
    public $test;
    public function index() {
        $this->test = 'test';
    }
}
通过调用用户函数数组(数组('example','index'),$params)调用类方法。我只能假设
call\u user\u func\u array
出于某种原因决定将index方法作为静态方法调用,例如
example::index()
?然而,我还没有找到解决方法,奇怪的是,直到最近我才发现它有问题。

这个方法有效:

$obj = new Example();
call_user_func_array(array($obj, 'index'), $params);
您的代码基本上可以:

Example::index($params);
它静态调用
索引
,您正确地假设了这一点。

这是有效的:

$obj = new Example();
call_user_func_array(array($obj, 'index'), $params);
您的代码基本上可以:

Example::index($params);

它静态调用
索引
,您正确地假设了这一点。

忘了提到我正在使用自动加载抱歉。我的印象是,调用user func数组将不需要手动实例化类。不是这样吗?@Riddian不。不是。实际上没有办法执行静态调用。顺便说一句:这种行为适用于所有接受回调的PHP函数(如等)。看,忘了提我正在使用自动加载抱歉。我的印象是,调用user func数组将不需要手动实例化类。不是这样吗?@Riddian不。不是。实际上没有办法执行静态调用。顺便说一句:这种行为适用于所有接受回调的PHP函数(如等)。看见