Php set_exception_handler()要求参数(exception_handler)为有效回调
我有以下代码:Php set_exception_handler()要求参数(exception_handler)为有效回调,php,error-handling,Php,Error Handling,我有以下代码: namespace Google\Ads\GoogleAds\Examples\BasicOperations; set_exception_handler('exception_handler'); function exception_handler($exception) { echo "Uncaught exception: " , $exception->getMessage(), "\n"; } 并得到以下错误(PHP7.3): 警告:set_excep
namespace Google\Ads\GoogleAds\Examples\BasicOperations;
set_exception_handler('exception_handler');
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
并得到以下错误(PHP7.3):
警告:set_exception_handler()需要参数
(异常处理程序)是有效的回调
似乎必须使用名称空间来引用异常处理程序函数,例如set_error_handler(“MyNamespace\my_error_handler”);-但是我还没有找到一个正确的例子。这是有效的:
<?php
namespace Google\Ads\GoogleAds\Examples\BasicOperations;
set_exception_handler('Google\Ads\GoogleAds\Examples\BasicOperations\exception_handler');
throw new \Exception('test');
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
这项工作:
<?php
namespace Google\Ads\GoogleAds\Examples\BasicOperations;
set_exception_handler('Google\Ads\GoogleAds\Examples\BasicOperations\exception_handler');
throw new \Exception('test');
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
您也必须使用其中的名称空间,这就是为什么它不起作用
set_exception_handler('Google\Ads\GoogleAds\Examples\BasicOperations\exception_handler');
而不是:
set_exception_handler('exception_handler');
您也必须使用其中的名称空间,这就是为什么它不起作用
set_exception_handler('Google\Ads\GoogleAds\Examples\BasicOperations\exception_handler');
而不是:
set_exception_handler('exception_handler');
或者,这也行得通
set_exception_handler(array($this,'exception_handler'));
或者,这也行得通
set_exception_handler(array($this,'exception_handler'));