The PHP Error Handler Class

Today we are going to look at a custom made error handler in PHP. We will create this error handler as a PHP class and it has to be able to handle two modes:

  1. When we develop we want as much detailed information as possible when a error occurs.
  2. When we release the web site we want to display less technical error messages and only respond with a friendly, user-understandable, message.

So, how do we solve this? In our case we will create two public methods: __construct() and handleError and a single private class variable $debug. We can do this Error Handler quite short and simple because PHP has already a built-in method supporting our task at hand. We start at looking upon the __construct() function.

OT: The __construct() function is the default function a class will activate when a class is instantiated. Another possible name of the __construct() function would be the name of the class itself. If we want to name our error handler class to ErrorHandler, our default function our class will activate on instantiation could be either __construct() or ErrorHandler().

To be able to handle our two modes, developing vs. running, we need a way to toggle between these modes easy. We do this by sending an argument to the __construct() function. This parameter can be 0 or 1. When applying 0 no detailed information will be shown, only user friendly messages and vice versa. This mode or state is saved in our private class variable $debug. The next thing our functions does is to use the built-in PHP method set_error_handler(). What we do here is saying, use ‘handleError’ to display the incoming errors and not your predefined function.

Code describing the __construct() function

Our second function is a bit longer, it’s our handleError() function. It takes four parameters: $errorType, $errorString, $errorFile and $errorLine. All these parameters will be sent to us by PHP, we don’t need to worry about nothing. The parameters themselves are rather easy to:

  • $errorType: will display what type of error code we are catching, we will create these.
  • $errorString: contains the error message,  we are able to modify this message.
  • $errorFile: let us know which file is causing the error.
  • $errorLine: where in the file the error has occured.

Sweet! So what we do first in the handleError() function is to use an outer switch looking at the $errorType. Depending on switch error type we have caught we should do different things. We will create our own error types further down, but for now we say we have FATAL, ERROR and WARNING as error types. When we enter one of these we do another switch depending on the private class variable $debug. Remember that variable held the current state/mode of the debug. 0 was to display user-friendly messages and 1 was to bring everything we got down and print it out. Basically the only difference we have between our FATAL, ERROR and WARNING in this example is that when we have a FATAL error we use exit to end the program. Both ERROR and WARNING will just display the warning but the script/page will still execute and continue.

Code describing the handleError() function.

Okay, so now we have our error handler class up and running. Now we need to define our FATAL, ERROR and WARNING error types. This will be done in our main page and not in the class file. In the main file we can declare as many error types as we want as long as the values we are assigning them to are of the built-in error handler types. I will display 7 different error warnings, they are displayed below in the following formation: value, constant, description

  • 2, E_WARNING, Non-fatal run-time errors. Execution of the script is not halted.
  • 8, E_NOTICE, Run-time notices. The script found something that might be an error, but could also happen when running a script normally.
  • 256, E_USER_ERROR, Fatal user-generated error. This is like an E_ERROR set by the programming using the PHP function trigger_error().
  • 512, E_USER_WARNING, Non-ftal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error().
  • 1024, E_USER_NOTICE, User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error().
  • 4096, E_RECOVERABLE_ERROR, Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle.
  • 8191, E_ALL, All errors and warnings, except level E_STRICT.

Click here to read more about the E_WARNINGS.

Code describing the define part.

What we do here is to define our FATAL, ERROR and WARNING and connect them to E_USER_ERROR, E_USER_WARNING and E_USER_NOTICE error reporting variables. At the very end we create an instance of our error handler class, sending a parameter with the value 1 to our __construct() function. This tells us that we want to display as much detailed information as possible. To be able to see if our class is working we will use the trigger_error() function found in PHP. If you read the list with the error reporting you notice that all three error reporting variables responds to just that function. Now we are able to write:

trigger_error('Database connection failed.', FATAL);

And our class will catch this and if we have supplied 0 as the debug parameter, it will display:

Sadly a fatal error has occured, please contact site administrator!

And there you have it! A rather simple Error Handler Class. In a few days I will release The PHP Series: Iteration #2 and I will use this Error Handler Class again. Make sure to study this and add it to the PHP series archive. Enjoy!


About this entry