Code
/**
* 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;
}
}
/**
* 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);
}
}
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;
}
