Proper error and exception display can save you a lot af time and nerves. A few months ago, Harry published a slick bluescreen script for exception handling. I modified it to handle errors as well and added some features which make it useful in productive systems as well:
- Error logging
- Error Mailing
- Configuration
The script logs or mails unique errors only once to prevent your log file or mailbox to be spammed with the same error again and again. It also takes care of the error level including shutup operator. It’s a little bit hacky but did well on our dev servers (where errors tend to happen) in the past few weeks.
To use it, download and include the two files and then setup the error and exception handler in your PHP script:
set_exception_handler(array('ErrorHandler', 'handleException'));
set_error_handler(array('ErrorHandler', 'handleError'));







Just to clarify this post. You will need to make sure you “require” these two files as well. Otherwise your code will not know what handleException or handleError are.
so add
August 12th, 2006 at 8:06 am
Wierd..it didn’t take my code. try #2.
require '/path/to/ErrorConfig.class.php'; require '/path/to/ErrorHandler.class.php';August 12th, 2006 at 8:07 am
hmm too bad so much of php land still uses php4? Would you be kind enough to port it or should I?
August 12th, 2006 at 2:22 pm
I’m keen to see a port aswell.
August 12th, 2006 at 8:16 pm
nsbucky, ccburns: What makes you think that it’s written for PHP 4?
August 13th, 2006 at 6:56 am
Maarten, I think they meant they were hoping for a port *to* PHP4.
August 13th, 2006 at 11:08 pm
Would be nice, I’m just not ready to switch to PHP5 right yet :)
August 14th, 2006 at 3:03 am
Porting this to PHP4 would be really difficult ;)
August 14th, 2006 at 11:00 pm
How long ago did PHP 5 come out…
August 19th, 2006 at 5:27 am
must have been 2 years ago or so.. most hosts support it now (at least as an option), you might have to name your files .php5 or something like that..
I dont see any reason to code in php 4 at all anymore.
August 22nd, 2006 at 1:31 am
Some hosts still don’t support php5 yet. I use hostgator and they’re still stuck on 4. Which sucks because I have 5 on my development server.
September 3rd, 2006 at 5:40 am
Will this Custom error handler be called regardless of the silencing operator??
September 20th, 2006 at 7:52 pm
Yes, it will be called, but the error will be discarded.
September 20th, 2006 at 7:57 pm
But it does not discardes at all…
I have added these lines:
public static function handleError($errno, $errstr, $errfile, $errline, $errcontext) { /* Check if error needs to be handled */ if(($errno & ErrorConfig::LEVEL) != $errno) { return; } if (error_reporting() == 0) { // print "(silenced) "; return; } /* Error types */ $error_types = array( 1 => 'ERROR', 2 => 'WARNING',September 20th, 2006 at 8:05 pm
I’m sorry, I thought that I’d committed the fix:
September 20th, 2006 at 10:17 pm
Nevermind, found the svn. Though it does not seem as the code is kept up to date? What is the license on the code?
March 28th, 2007 at 4:34 pm
Can you please reupload the two files for this post?
Thanks
August 27th, 2007 at 10:53 am