Reputation: 379
I have the following code:
namespace Google\Ads\GoogleAds\Examples\BasicOperations;
set_exception_handler('exception_handler');
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
And get the following error (PHP 7.3):
Warning: set_exception_handler() expects the argument (exception_handler) to be a valid callback
It seems like the namespace has to be used to reference the exception handler function - such as set_error_handler("MyNamespace\my_error_handler"); - but I have not found an example that works correctly.
Upvotes: 1
Views: 1455
Reputation: 3128
You have to use the namespace in there too,that's why it was not working.
set_exception_handler('Google\Ads\GoogleAds\Examples\BasicOperations\exception_handler');
instead of :
set_exception_handler('exception_handler');
Upvotes: 1
Reputation: 471
Alternatively, this works too
set_exception_handler(array($this,'exception_handler'));
Upvotes: 1
Reputation: 3341
This works:
<?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";
}
Upvotes: 0