Tips on Using PEAR_Error
| From: | Michael | Date: | Tue, 28 Jan 2003 20:29:10 +0000 |
| Subject: | Tips on Using PEAR_Error | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-3504@lists.php.net to get a copy of this message | ||
Hi,
I am looking to use multiple PEAR packages in a web application framework. One thing I am a bit stumped with is how to best generalize error handling between PEAR packages and other classes. Ultimately, I want to have all raised errors passed to a error handler class which takes appropriate action (emails error, outputs to screen, log, etc..)
To the best of my understanding of the architecture of PEAR_Error, I simply have to extend my error handler class with PEAR and statically define the callback function (which in turn calls my error handler object) with PEAR::setErrorMode(PEAR_ERROR_CALLBACK, "my_function_handler"); I am most likely not doing this right, as when I raise a PEAR error, it does not init my callback function.
Any help woudl be appreciated. A sample of my code follows.
Michael
<?php
require_once('PEAR.php');
class ErrorHandler extends PEAR
{
function ErrorHandler()
{
$this->PEAR();
}
function HandleError()
{
$args = func_get_args();// do stuff... } } function errorHandler() { static $EH = ""; if(!error_reporting()) // Check for @ error control operator return 0; if ( empty($EH) ) {
foreach ( array_keys($GLOBALS) as $key )
{
if ( is_object( $GLOBALS[$key] ) )
{
if ( 'errorhandler' == get_class($GLOBALS[$key]) )
{
$EH =& $GLOBALS[$key];
break;
}
}
}
}
if ( func_num_args() > 0 )
{
$args = func_get_args();
call_user_func_array(array(&$EH, "HandleError"), $args);
}
else {
return $EH;
}
}
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'ErrorHandler');
$fw_error = & new ErrorHandler();
trigger_error("test", E_USER_ERROR);
//Does not call errorHandler unless I define it with set_error_handler()
class test extends PEAR
{
function test($string = null)
{
$this->PEAR();
}
function run($string = null)
{
if (is_null($string))
return $this->raiseError("Error");
//raised error does not call the call back function
}} $test = & new test(); $res = $test->run(); if (PEAR::isError($res))
echo $res->getMessage( );?>