How to catch undefined functions with set_error_handler in PHP

Asked
Active3 hr before
Viewed126 times

7 Answers

catchhandlererrorfunctionsundefined
90%

Calling undefined functions triggers an E_ERROR, thus it can not be handled by the error callback (or by exception handlers for that matter). All that you can do is set error_reporting to 0.,set_error_handler is designed to handle errors with codes of: E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE. This is because set_error_handler is meant to be a method of reporting errors thrown by the user error function trigger_error.,Very interesting thing that I've discovered today as I was facing the similar problem. If you use the following - it will catch the error with your custom error handler function / method:,At a guess, I'd say that undefined function errors travel through a different execution path than other error types. Perhaps the PHP designers could tell you more, except I doubt PHP is in any way designed.

This is not exactly true. set_error_handler() can't handle them, but ob_start() can handle at least E_ERROR.

< ? php

function error_handler($output) {
   $error = error_get_last();
   $output = "";
   foreach($error as $info => $string)
   $output. = "{$info}: {$string}\n";
   return $output;
}

ob_start('error_handler');

will_this_undefined_function_raise_an_error();

?
>
load more v
88%

Sets a user function (callback) to handle errors in a script. ,set_error_handler — Sets a user-defined error handler function, If the function returns false then the normal error handler continues. ,restore_error_handler() - Restores the previous error handler function

vector a
Array
(
[0] => 2
[1] => 3
[2] => foo
[3] => 5.5
[4] => 43.3
[5] => 21.11
)
----
vector b - a notice (b = log(PI) * a)
<b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />
Array
(
[0] => 2.2894597716988
[1] => 3.4341896575482
[2] => 0
[3] => 6.2960143721717
[4] => 49.566804057279
[5] => 24.165247890281
)
----
vector c - a warning
<b>My WARNING</b> [512] Incorrect input vector, array of values expected<br />
NULL
----
vector d - fatal error
<b>My ERROR</b> [256] log(x) for x
<= 0 is undefined, you used: scale=-2.5<br />
Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br />
Aborting...<br />
72%

However, this setup doesn't work for undefined functions.,Sorry, an error has occurred on line 17. The function that caused the error says Undefined variable: imAFakeVariable.,Why isn't my custom error handler catching undefined functions? Are there other problems that this will cause?,Fatal error: Call to undefined function: imafakefunction() in /Library/WebServer/Documents/experimental/errorhandle.php on line 17

I'm testing it out on a practice page:

function customError($level,$message,$file,$line,$context) {
    echo "Sorry, an error has occured on line $line.<br />";
    echo "The function that caused the error says $message.<br />";
    die();
}

set_error_handler("customError");

echo($imAFakeVariable);

However, this setup doesn't work for undefined functions.

function customError($level,$message,$file,$line,$context) {
    echo "Sorry, an error has occured on line $line.<br />";
    echo "The function that caused the error says $message.<br />";
    die();
}

set_error_handler("customError");

imAFakeFunction();
load more v
65%

I'm taking the leap: my PHP scripts will ALL fail gracefully!,Why isn't my custom error handler catching undefined functions? Are there other problems that this will cause?,However, this setup doesn't work for undefined functions.,Sorry, an error has occurred on line 17. The function that caused the error says Undefined variable: imAFakeVariable.

I'm testing it out on a practice page:

function customError($level,$message,$file,$line,$context) {
    echo "Sorry, an error has occured on line $line.<br />";
    echo "The function that caused the error says $message.<br />";
    die();
}

set_error_handler("customError");

echo($imAFakeVariable);

However, this setup doesn't work for undefined functions.

function customError($level,$message,$file,$line,$context) {
    echo "Sorry, an error has occured on line $line.<br />";
    echo "The function that caused the error says $message.<br />";
    die();
}

set_error_handler("customError");

imAFakeFunction();

This is not exactly true. set_error_handler() can't handle them, but ob_start() can handle at least E_ERROR.

< ? php

function error_handler($output) {
   $error = error_get_last();
   $output = "";
   foreach($error as $info => $string)
   $output. = "{$info}: {$string}n";
   return $output;
}

ob_start('error_handler');

will_this_undefined_function_raise_an_error();

?
>
load more v
75%

This is not exactly true. set_error_handler() can't handle them, but ob_start() can handle at least E_ERROR.,"The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.",set_error_handler is designed to handle errors with codes of: E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE. This is because set_error_handler is meant to be a method of reporting errors thrown by the user error function trigger_error.,Understand how to register domain names and put your new site on the web!

This is not exactly true. set_error_handler() can't handle them, but ob_start() can handle at least E_ERROR.

 $string)
 "        $output .= " {
    $info
 }: {
    $string
 }
 "";
 "    return $output;"
 }
 ""
 ob_start('error_handler');
 ""
 will_this_undefined_function_raise_an_error();
 "" ? > "
load more v
40%

PHP has a lot of constants that are used in relation to errors. These constants are used when configuring PHP to hide or display errors of certain classes.,E_ERROR – the script cannot continue running and it is being terminated. Will throw errors and how these are going to be handled depends on the error handler.,The function restore_exception_handler() will revert the exception handler to its previous value.,E_RECOVERABLE_ERROR – it indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. Further execution depends on the error handler and error will definitely be thrown.

While PHP 7 provides both Error and Exception classes, let’s check the Throwable interface first. Both Error and Exception classes implement Throwable interface – it's a basis for any object that can be thrown via throw statement. It only has one exception to notice, it cannot be implemented in userland classes directly, but only through extending the Exception class. Also, it provides a single point for catching both types of error in a single statement:

< ? php

try {
   // your code
} catch (Throwable $e) {
   echo 'Very nice way to catch Exception and Error exceptions';
}
load more v
22%

Example 1. Error handling with set_error_handler() and trigger_error() , See also error_reporting(), restore_error_handler(), trigger_error(), and error level constants. ,Note: If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time. , Also note that it is your responsibility to die() if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error.

vector a
Array
(
[0] => 2
[1] => 3
[2] => foo
[3] => 5.5
[4] => 43.3
[5] => 21.11
)
----
vector b - a warning (b = log(PI) * a)
<b>WARNING</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />
Array
(
[0] => 2.2894597716988
[1] => 3.4341896575482
[2] => 0
[3] => 6.2960143721717
[4] => 49.566804057279
[5] => 24.165247890281
)
----
vector c - an error
<b>ERROR</b> [512] Incorrect input vector, array of values expected<br />
NULL
----
vector d - fatal error
<b>FATAL</b> [256] log(x) for x
<= 0 is undefined, you used: scale=-2.5<br />
Fatal error in line 36 of file trigger_error.php, PHP 4.0.2 (Linux)<br />
Aborting...<br />

Other "catch-handler" queries related to "How to catch undefined functions with set_error_handler in PHP"