<?php function exception_error_handler($errno, $errstr, $errfile, $errline ) { throw new ErrorException($errstr, 0, $errno, $errfile, $errline); } set_error_handler("exception_error_handler"); ?>The function name passed to set_error_handler will be called if there is ever a PHP error. The function set up to handle errors takes the error information, constructs an ErrorException and then throws it. Errors never go directly out to the user or logs, they are always wrapped in an exception. Doing this give you greater control over error handling.
In the user comments on the ErrorException page troelskn gives a suggestion for improvement to the basic example above. He suggests the following code.
<?php function exception_error_handler($severity, $message, $filename, $lineno) { if (error_reporting() == 0) { return; } if (error_reporting() & $severity) { throw new ErrorException($message, 0, $severity, $filename, $lineno); } } set_error_handler('exception_error_handler'); ?>This code allows the error reporting value set in php.ini to effect the behavior of the exception_error_handler. Only errors of a high enough severity will be passed on as exceptions. Using his code, if you have warnings suppressed, they will not raise an exception. They will just not be reported. I hope this removes one more of the little PHP quarks for you.
1 comment:
you have a very eclectic selections of posts
Post a Comment