Back to the blog
Recent Posts
-
Dec 05
Twitter user streams with python
-
Nov 30
Fix Firefox 5+ font rendering in Windows
-
Aug 31
A bit of icon work
-
Jun 24
Your mobile OS can't really multitask...
-
Apr 27
Thoughts on Android and the HTC Dream
-
Feb 26
Announcing Twitterscribe: archive your tweets
Most Popular Posts
-
Why you should be using a framework
-
Dynamic methods in PHP
-
Rewriting URLs with Apache's mod_rewrite and PHP
-
Five easy things that make you a better web developer
About the Blog

I'm a web application developer in Melbourne, Australia. If you find anything useful, leave me a comment, and if you need web design, development, or accessibility and usability consulting, contact me! Cheers.
Twitter: joshsharp
Turning PHP errors into Exceptions
Sunday 11 Nov, 2007 08:04 PM
I've just been browsing the PHP6 meeting minutes to see if anything exciting is coming up in the next release. There are a couple of handy things which I might post about soon, but in the meantime this useful code snippet caught my eye. I wasn't aware you could handle errors yourself in PHP5, but it seems it is possible to do so. The following catches all errors of level E_NOTICE and throws an exception instead. Pretty handy if you already use Exceptions for your own errors — now you can decide what to do with parser errors, instead of being forced into displaying them or just ignoring them.
The code:
<?php
function error_handler($errorType, $message){
if ($errorType == E_NOTICE) {
throw new Exception( $message, $errorType);
}
}
set_error_handler('error_handler');
?>