Contributed by David Mytton
Print
Email
Create your own PHP error handling class
If you are creating your own application or website, you want to be able to handle the errors that occur during development to display as much information as possible. On the other hand, once development is completed, you do not want to show a massive set of error data to your visitors. Quite the opposite - you want to show a friendly message.
The default PHP error handler is good that it shows some useful information for developers. But to a user who is not familiar with PHP, or even one that is, an error can be annoying and confusing. When visiting a website, you expect everything to work and when it doesn't, you will often get frustrated. As a site owner or script developer, you should want to reduce the frustration suffered by your users if there ever was a problem.
This is where creating your own error handling class comes in. Your class will take over the error handling from PHP and allow you to customise the way errors are displayed to your users. You can create your own design and change the output depending on whether you are developing (or debugging) showing users a nice message whilst showing you useful error information.
The Class
The class itself only contains 2 functions. The first one is a constructor which is called as soon as you instantiate the class object. This function calls the set_error_handler() function. This funcion "sets a user-defined error handler function" and is used to direct all error messages triggered to your own error handling function - handle_error(). This function will take several parameters which provide some extra data for you to use. The function alows displays the friendly error message to the screen and halts the execution of the script. In our function, we're going to add an extra feature - a debug level - which will be defined when you instantiate the class. This will decide which kind of output is shown to the user - developer or user friendly. So let's have a look at the function.
errors()
In this case, our class is called errors and has 1 class variable called $debug_level. The function is named the same as the class and is therefore a constructor.
{
// Populate class variables
$this->debug_level = $debug_level;
// Take over from PHP error handling
set_error_handler(array($this, 'handle_error'));
}
On the first line, we are declaring our function and providing a default value for $debug_level in case it is not set when the object is created. The debug level is then stored in the class varible for use in our error handling function. Finally, error handling is taken over by the function called handle_error() - which is the 2nd parameter in the set_error_handler() function. The 1st parameter passes any variables we have set to the error handling function.
handle_error()
This function actually handles any errors that are triggered during script execution. It accepts 5 parameters which contain data for use when debugging - error type, error description string, file name, line number and variable dump.
Firstly, the error type decides what will happen when the error is triggered. By default, there are 3 error types - FATAL, WARNING and NOTICE. You may recognise these from your PHP programming. You can define your own if you wish. This is achieved by declaring them as constants when you instantiate the class, which will be covered later.
When you trigger the error (also covered later), you will pass a description which can be displayed when you handle the error. The file name, line number and variable dump are automatically passed to the function when the error is triggered so you can display them when you handle the error.
The bare code for this function is as follows:
{
// Decide which type of error it is, and handle appropriately
switch ($type)
{
// Error type
case FATAL:
// Select debug level
switch ($this->debug_level)
{
default:
case 0:
echo 'Error: '.$string.' in '.$file.' on line'. $line.'<br />';
print_r($var);
// Stop application
exit;
case 1:
echo 'There has been an error. Sorry for the inconvenience.';
// Stop application
exit;
}
case ERROR:
echo '<pre><b>ERROR</b> ['.$type.'] '.$string.'<br />'."</pre>n";
break;
case WARNING:
echo '<pre><b>WARNING</b> ['.$type.'] '.$string.'<br />'."</pre>n";
break;
}
}
The first switch() statement is checking what kind of error we are triggering. As I mentioned before, you can define your own error types and handle them in a different way. I shall be concentrating on the FATAL error type which is the most common. This will display a message to the user and then exit the script execution.
Once in the case statement for the FATAL error type, we are then checking for the debug level. This is where you can choose what to display to your users based on the debug level. If set to 0, you see a full error message - the error, the file and line. The var dump is also included. However, if you set the debug level to 1, you just get a friendly error; which you would of course customise for yourself. You could also include some code to make it send an e-mail with the details of the error to you.
Back to the class
To bring this all together into the errors class, you have the following:
{
var $debug_level = 0;
function errors($debug_level = 0)
{
// Populate class variables
$this->debug_level = $debug_level;
// Take over from PHP error handling
set_error_handler(array($this, 'handle_error'));
}
function handle_error($type, $string, $file, $line, $vars)
{
// Decide which type of error it is, and handle appropriately
switch ($type)
{
// Error type
case FATAL:
// Select debug level
switch ($this->debug_level)
{
default:
case 0:
echo 'Error: '.$string.' in '.$file.' on line'. $line.'<br
/>';
print_r($var);
// Stop application
exit;
case 1:
echo 'There has been an error. Sorry for the
inconvenience.';
// Stop application
exit;
}
case ERROR:
echo '<pre><b>ERROR</b> ['.$type.'] '.$string.'<br />'."</pre>n";
break;
case WARNING:
echo '<pre><b>WARNING</b> ['.$type.'] '.$string.'<br
/>'."</pre>n";
break;
}
}
}
Using the Error Handling
Now you have your class set up, you actually need to use it. The first step is to instantiate the class. This is very simple as the class accepts 1 parameter - the debug level: 0 for development use and then 1 when you go live:
define('ERROR', E_USER_WARNING);
define('WARNING', E_USER_NOTICE);
$errors = new errors(1);
You must include the 3 define statements to define the default error types.
Error Triggering
In order to actually utilise the functions in this class, you need to trigger the error handling function. This is done using the PHP function trigger_error() like so:
To to trigger a fatal error:
And that concludes the tutorial on error handling!
By David Mytton

Site
Management
Index