ARTICLE :
 

Centralized Error / Exception handling in PHP

by SBajra on June 20th, 2011

Introduction

This article is to cover error and exception handling in PHP. This article concentrates more on centralized exception handling technique in PHP. Centralized exception handling comes handy because we just have to define error / exception handler in one central location and it takes care of error/exception handling in every line of code in entire website. End result would be no unhandled error or exception.

Background

PHP, unlike any other programming language supports 1) core "Error" and 2) object oriented style "Exception". For any problem with PHP code, we normally get PHP error. PHP exceptions are normally thrown by user code. We can however trigger user error using trigger_error() function. This article explains how we can define central error and exception handling mechanism so that regardless of error or exception in the code, it will get caught and handled centrally in standard unified way.

Detail

Lets start with a PHP error. For any programming or system error, PHP triggers error. Error in php is different from Exception. Exception is another way to throw error in object oriented style. Exception is a standard class in php and can be caught using try...catch... block. However, PHP does not support "finally".

As I mentioned earlier, for anything wrong PHP triggers error. Error in PHP can be handled using set_error_handler() method. Now, for the central unified error and exception handling, we will convert error to standard exception and handle both error and exception centrally using ExceptionHandler using set_exception_handler() method.

For error handling:
//Error handling
function ErrorHandler($errLevel, $errMsg, $errFile, $errLine)
{
	throw new ErrorException($errMsg, $errLevel, $errLevel, $errFile, $errLine);
}

set_error_handler("ErrorHandler");
For any error, it will call error handler function "ErrorHandler" with parameters such as errorLevel, errorMessage, errorFilename and errorLineNumber. This error handler function converts error to ErrorException() and throw it back. Now, every error will get converted to ErrorException() object.

For more detail on PHP set_error_handler() function
http://us2.php.net/manual/en/function.set-error-handler.php

For more detail on PHP ErrorException class:
http://us2.php.net/manual/en/class.errorexception.php

Then, we write code to handle all exception:
//Exception handling
function ExceptionHandler($exception)
{
	try 
	{
    	//Getting exception information
		$errMsg = $exception->getMessage();
		$errLevel = $exception->getCode();
		$errFile = $exception->getFile();
		$errLine = $exception->getLine();
        
    	//Logging error information to log file or database		
        // TO - DO
	}
	catch (Exception $ex)
	{
		//Do nothing. 
		//Avoiding error from error handling...
	}
	
	//Redirect to Error page
	header("Location: /error.php");
    
	/* Don't execute PHP internal error handler */
    return true;
}
//Setting Exception Handler
set_exception_handler('ExceptionHandler');
In this code, every exception is handler inside ExceptionHandler() method. Inside ExceptionHandler() method, error/exception detail is logged to log file or database and then redirected to error page (say error.php).

For more detail on PHP set_exception_handler() function
http://us2.php.net/manual/en/function.set-exception-handler.php

Now, we add all this error/exception handling code into a separate include file say "errorhandler.php" then include this "errorhandler.php" in every php page.

For example, lets create a test php page say "test.php":
<?php

require_once("errorhandler.php");

echo "this is test php page";

?>

Conclusion

In this way, we can define centralized unified standard error and exception handling process which catch all errors and exceptions, write to log file or database and then finally redirect to an error page.

Download

Click here to download source code

Author:

Profile Photo
SBajra
Date: June 20th, 2011

Comments: