DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Converting Old PHP Errors Into Exceptions

03.02.2006
| 27908 views |
  • submit to reddit
        The PHP5 Exception class will not work correctly. It would reflect an incorrect line number and file name and not record the 'errcontext' value associated with PHP errors. To solve this problem, we must extend the Exception class.
/**
 * This exception behaves like a "old school" PHP Error
 */
class STEM_ErrorException extends Exception
{
	/**
	 * The PHP Error Context
	 *
	 * The fifth parameter is optional, errcontext, which is an array that points to the active symbol table at the point the error occurred. In other words, errcontext  will contain an array of every variable that existed in the scope the error was triggered in. User error handler must not modify error context.
	 */
	private $m_arContext;

	/**
	 * Constructor
	 */
	public function __construct($vMessage, $vCode, $vFile, $vLine, $arContext = null)
	{
		parent::__construct($vMessage, $vCode);
		
		$this->file = $vFile;
		$this->line = $vLine;
		
		$this->m_arContext = $arContext;
	}
}

We need to define a function to handle errors. We also want to write two helper functions that will set and restore the error handler to our function. To do this, we write three methods in an abstract class.
/**
 * STEM Error Handler
 *
 * Registers Itself as a PHP Error Handler and proceeds to convert all
 * native "old school" PHP errors into new PHP5 Exceptions.
 *
 * Call STEM_ErrorHandler::Initialize(); before your try blocks and
 * STEM_ErrorHandler::Uninitialize(); afterwards.
 */
abstract class STEM_ErrorHandler
{
	/**
	 * Encapsulates set_error_handler()
	 */
	public static function Initialize()
	{
		set_error_handler(array("STEM_ErrorHandler", "HandleError"));
	}
	
	/**
	 * Encapsulates restore_error_handler()
	 */
	public static function Uninitialize()
	{
		restore_error_handler();
	}
	
	/**
	 * Handles PHP Errors
	 */
	public static function HandleError($errno, $errstr, $errfile, $errline, $errcontext)
	{
		throw new STEM_ErrorException($errstr, $errno, $errfile, $errline, $errcontext);
	}
}

To test it, we create a file that triggers errors. Before triggering the first error, we call Initialize(). We then Uninitialize() the error handler and trigger the error again to check that our error handler has been removed. It is important to check the line numbers mentioned in the message that this prints!
STEM_ErrorHandler::Initialize();

try
{
	trigger_error("Hello World!");
}
catch (Exception $e)
{
	print $e;
}

STEM_ErrorHandler::Uninitialize();
print "<hr />";

try
{
	trigger_error("Hello World!");
}
catch (Exception $e)
{
	print $e;
}

-- 
Version 0.1.0 - 2006-02-14
STEM: The STEM Cells of PHP
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
http://creativecommons.org/licenses/by-sa/2.5/