Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 set_exception_handler()要求参数(exception_handler)为有效回调_Php_Error Handling - Fatal编程技术网

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'));