Set custom error handlers in PHP

Views: 34 Last modified: August 03rd, 2011 Comments: 0

If you desire a custom error handler, you have to use set_error_handler() because you need to let PHP know you want to use a custom error handler. If you followed up on this function, you have to define the actual function which has been set along the set_error_handler() callback. You are allowed to set different error handlers for different types of PHP errors.

The custom error handler can use at least 4 default arguments.

  • Error number
  • Error message
  • Error file
  • Error line

Three examples of custom defined error handlers for Notices, Warnings and Deprecated functions

<?php
// Set error handler E_NOTICE
set_error_handler('NoticeHandler', E_NOTICE);

// Warning - Notice handler
function NoticeHandler ($errnr, $errmsg, $errfile, $errline){
    echo 'The function '. __FUNCTION__ .', picked up the notice and reports the following:'.
            '<br />Error number: '. $errnr.
            '<br />Error message: '. $errmsg.
            '<br />Error line: '. $errline.
            '<br />Error file: '. $errfile;
}

// Trigger the error
echo $undefined_variable;

// Set error handler E_WARNING
set_error_handler('WarningHandler', E_WARNING);

// Warning - Warning handler
function WarningHandler ($errnr, $errmsg, $errfile, $errline){
    echo '<br /><br />The function '. __FUNCTION__ .', picked up the warning and reports the following:'.
            '<br />Error number: '. $errnr.
            '<br />Error message: '. $errmsg.
            '<br />Error line: '. $errline.
            '<br />Error file: '. $errfile;
}

file_get_contents('no/not/there.php');

// Set error handler E_DEPRECATED
set_error_handler('DeprecatedHandler', E_DEPRECATED);

// Warning - Error handler
function DeprecatedHandler ($errnr, $errmsg, $errfile, $errline){
    echo '<br /><br />The function '. __FUNCTION__ .', picked up the error and reports the following:'.
            '<br />Error number: '. $errnr.
            '<br />Error message: '. $errmsg.
            '<br />Error line: '. $errline.
            '<br />Error file: '. $errfile;
}

echo session_register('Registered');
?>

The result

The function NoticeHandler, picked up the notice and reports the following:
Error number: 8
Error message: Undefined variable: undefined_variable
Error line: 30
Error file: C:\server\htdocs\tests\test.php

The function WarningHandler, picked up the warning and reports the following:
Error number: 2
Error message: file_get_contents(no/not/there.php) [function.file-get-contents]: failed to open stream: No such file or directory
Error line: 46
Error file: C:\server\htdocs\tests\test.php

The function DeprecatedHandler, picked up the error and reports the following:
Error number: 8192
Error message: Function session_register() is deprecated
Error line: 61
Error file: C:\server\htdocs\tests\test.php1


This list of error constants could be used for assigning a custom error handler to. Excluding the following list:

  • E_ERROR
  • E_PARSE
  • E_CORE_ERROR
  • E_CORE_WARNING
  • E_COMPILE_ERROR
  • E_COMPILE_WARNING
  • E_STRICT
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {0+4} =  
    Anything to add ?

        You must be logged in to post a comment.