Recursive. Custom web app development

Back to the blog

Recent Posts

Most Popular Posts

  1. Why you should be using a framework
  2. Dynamic methods in PHP
  3. Rewriting URLs with Apache's mod_rewrite and PHP
  4. Five easy things that make you a better web developer

About the Blog

Self portrait

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

a bird

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');
?>
Tags: php

Comments

Tristan Kenney said on 12 Nov 2007:
PHP is becoming the language I always knew it could be. Mostly.