Page 1 of 1

Create a simple error handler Rate Topic: ***** 1 Votes

#1 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Post icon  Posted 20 February 2009 - 09:07 AM

Everyone at some point has gotten that ugly PHP error message when visiting a website on the web, or seen a PHP warning message nesting at the top of the page, well in this tutorial I will show you a way that you can handle error's yourself and only display the error messages on screen when you are debugging your application.

First thing we are going to do within our class, is set up the objects and the constructor. Like so:
<?php
class ErrorHandler
{
	private $debug = 0;

	public function __construct($debug = 0)
	{
		$this->debug = $debug;
		set_error_handler(array($this, 'generalError'));
	}



Lets break this down, first off since PHP 5 the keyword var has been deprecated and is replaced by one of the following 3: protected, private and public. You can look up more information on Google and PHP's website. We set the debug object to 0 (off) as a default, the next thing we do is make our constructor, in that we pass a parameter for the debug mode, then we tell PHP that we want our function “generalError” to handle all of PHP's errors.

Next thing that needs to be done is to create the “generalError” function:
	public function handleError($type, $string, $file, $line)
	{	
		switch ($type)
		{
			case FATAL:
				switch ($this->debug)
				{
			case 0:
							echo 'We have an error!';
							exit;
						case 1:
							echo "<pre><b>FATAL</b> [T: $type] [L: $line] [F: $file]<br />$string<br /></pre>";
								exit;
				}
			case ERROR:
				echo "<pre><b>ERROR</b> [T: $type] [L: $line] [F: $file]<br/>$string<br /></pre>";
				break;
			case WARNING:
				echo "<pre><b>WARNING</b> [T: $type] [L: $line] [F: $file]<br/>$string<br /></pre>";
				break;
		}
	}
} 
?>


Basically what we are doing here is receiving the four standard strings that PHP passes when it receives a warning or an error. Next thing we are doing is a switch statement to see what type of problem we are having, and if it is a fatal error we first see if we are in debug mode before we display the error because if we aren't in debug mode we only an to let the user see a little bit of information, if we are the testers need to know where to look.

Here is a little test file that you can use to test it:
<?php

	require('ErrorHandler.php');

	

	define('FATAL', E_USER_ERROR);

	define('ERROR', E_USER_WARNING);

	define('WARNING', E_USER_NOTICE);



	$errorHandler = new ErrorHandler(1);

?>


Also if you want to trigger your own errors you would want to look up the trigger_error() funtion.

Finally the ways that you could improve this simple class is to create functions that would handle different problems, like database based errors, file errors etc.

Is This A Good Question/Topic? 0
  • +

Replies To: Create a simple error handler

#2 lemonacode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 02-February 09

Posted 22 February 2009 - 05:49 AM

This is awesome, very easy to understand, helped me alot with my function error handling.
Thank you Auzzie :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1