Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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
在PHP8中使用call_user_func返回致命错误_Php_Php 8 - Fatal编程技术网

在PHP8中使用call_user_func返回致命错误

在PHP8中使用call_user_func返回致命错误,php,php-8,Php,Php 8,我试图调用名为ContactController的类,其函数为handleContact withcall_user_func([ContactController::class,'handleContact')但出现以下错误: 致命错误:未捕获错误:无法静态调用非静态方法app\controllers\ContactController::handleContact() 如果handleContact方法是静态的,则调用它: call_user_func([ContactController:

我试图调用名为ContactController的类,其函数为handleContact with
call_user_func([ContactController::class,'handleContact')但出现以下错误:

致命错误:未捕获错误:无法静态调用非静态方法app\controllers\ContactController::handleContact()


如果handleContact方法是静态的,则调用它:

call_user_func([ContactController::class, 'handleContact'])
如果您的方法handleContact不是静态的,那么您需要传递一个实例化的类,例如

$contactController = new ContactController();
call_user_func([$contactController, 'handleContact'])

另外,将handleContact设置为static将是一个简单的解决方法。

如果handleContact方法是static,则调用它:

call_user_func([ContactController::class, 'handleContact'])
如果您的方法handleContact不是静态的,那么您需要传递一个实例化的类,例如

$contactController = new ContactController();
call_user_func([$contactController, 'handleContact'])

另外,将handleContact设置为静态将是一个简单的方法。

由于
handleContact
不是静态方法,您应该首先实例化您的
ContactController
,然后在创建的实例上调用该函数

在现代PHP中,您甚至不需要使用丑陋的
call\u user\u func()
,因为您可以通过
()
直接调用回调


但是,如果您的
handleContact
方法没有直接对
ContactController
实例进行操作(即不使用
$this
变量),您可以使用方法定义中的
static
关键字将方法转换为static,并且您的原始代码应该可以工作。

由于
handleContact
不是静态方法,您应该先实例化
ContactController
,然后在创建的实例上调用该函数

在现代PHP中,您甚至不需要使用丑陋的
call\u user\u func()
,因为您可以通过
()
直接调用回调


但是,如果您的
handleContact
方法没有直接在
ContactController
实例上操作(即不使用
$this
变量),您可以使用方法定义中的
static
关键字将方法转换为static,您的原始代码应该可以工作。

感谢您的反馈。不幸的是我把它贴错了。我调用的函数与您描述的相同。我已经调整了我的问题以反映这一点。我刚刚更新了我的回答。试试看,希望有帮助。非常感谢。使用此解决方案效果很好谢谢您的反馈。不幸的是我把它贴错了。我调用的函数与您描述的相同。我已经调整了我的问题以反映这一点。我刚刚更新了我的回答。试试看,希望有帮助。非常感谢。它与此解决方案配合得很好