04-12-2009, 12:22 AM
|
#53 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
For error handling I used a simple way;
- I have a function to store global shared variables, my implementation of such a function is similar to how an extension global is in C in the php source:
PHP Code:
function PG($name, $value = NULL)
{
static $variables;
if(!$variables)
{
$variables = Array();
}
if(func_num_args() > 1)
{
$variables[$name] = $value;
}
elseif(!array_key_exists($name, $variables))
{
return(false);
}
return($variables[$name]);
}
Then I allocate a default element to carry the non fatal errors, notice that this is an ArrayObject and now just a regular Array(). This is due to PHP doesn't support function dereferencing.
PHP Code:
PG('errors', new ArrayObject);
- In the boostraper I also define a custom error and exception handler, set by set_[error|exception]_handler. The exception handler will check if an exception is fatal or not (depending on which one you send):
PHP Code:
function dingo_exception_handler(Exception $e)
{
/* Simply consider any exception not of the default exception class fatal */
if(get_class($e) == 'Exception')
{
PG('errors')->append($e->getMessage());
return;
}
/* Format the error message, if you are in debug mode you can expose the path and line number (requires 'PG('debug')' defined */
$format = '%s';
if(PG('debug'))
{
$format = '%s in %s on line %d';
}
$message = sprintf('%s' . (PG('debug') ? ' in %s on line %d' : '')), $e->getMessage(), $e->getFile(), $e->getLine());
/* $dingo->set_template_and_vars(), $dingo->view() */
}
And the same with the error handler:
PHP Code:
function dingo_error_handler($severity, $message, $file = NULL, $line = NULL)
{
/* Respect the current error_reporting setting */
if(!(error_reporting() & $severity))
{
return;
}
$fatal = false;
switch($severity)
{
case(E_RECOVERABLE_ERROR):
{
$prefix = 'Recoverable error';
$fatal = true;
}
break;
case(E_USER_ERROR):
{
$prefix = 'Fatal error';
$fatal = true;
}
break;
case(E_NOTICE):
case(E_USER_NOTICE):
{
$prefix = 'Notice';
}
break;
/* E_DEPRECATED & E_USER_DEPRECATED, available as of PHP 5.3 - Use their numbers here to prevent redefining them and two E_NOTICE's */
case(8192):
case(16384):
{
$prefix = 'Deprecated';
}
case(E_STRICT):
{
$prefix = 'Strict standards';
}
break;
default:
{
$prefix = 'Warning';
}
}
$error = sprintf('%s: %s' . (PG('debug') ? ' in %s on line %s' : ''), $prefix, $message, ($file ? $file : 'unknown'), ($line ? $line : 'unknown'));
if($fatal)
{
/* Handle a fatal error here, like exceptions */
}
elseif(PG('debug'))
{
/* Only add these non fatal errors to the error stack if we're in debug mode to prevent the user from seeing these */
PG('errors')->append($error);
}
}
- When compiling the errors into an error message you can do like (basic HTML):
PHP Code:
$html = 'No errors!';
if(($errors = PG('errors')) && sizeof($errors))
{
$html = '<h1>Errors</h1>' .
'<ul>';
foreach($errors as $error)
{
$html .= '<li>' . $error . '</li>';
}
$html .= '</ul>';
}
echo($html);
Hope this is easy enough to understand with the comments (as its 2:22 here, I better get some sleep)
ps. as a quick remark about option #3 in your previous post; you cannot use the name "use", its a reserved keyword as of PHP 5.3 ;)
__________________
Last edited by Kalle : 04-12-2009 at 01:56 AM.
|
|
|