PHP errors as exceptions
Posted: Wed Nov 04, 2009 8:23 pm
I am stuck. I am trying to figure out how to setup a class that will handle PHP errors as exceptions. I reviewed the PHP Manual, specifically the "ErrorException" class, but I am having a hard time figuring out how to implement the method without having to call the "set_error_handler()" from outside a class. The code below what what I copied from the PHP manual:
Can someone shed some light on this? Thanks in advance for your help.
Code: Select all
<?php
class ErrorHandler extends Exception {
protected $severity;
public function __construct($message, $code, $severity, $filename, $lineno) {
$this->message = $message;
$this->code = $code;
$this->severity = $severity;
$this->file = $filename;
$this->line = $lineno;
}
public function getSeverity() {
return $this->severity;
}
}
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorHandler($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler", E_ALL);
function A() {
$foo->bar; // Purposely cause error
}
function B($c) {
A();
}
try {
B('foobar');
} catch (Exception $e) {
var_dump($e->getTrace());
}
?>