I currently have two class hierarchies: one for handling errors and another for logging them. I am wondering 1) whether it is advantageous to delegate error handling to a class separate from the error handler and 2) whether what I have is a good design.
Here is what I have:
class ErrorHandler {
public __construct(ErrorLogger $ErrorLogger) {
...
$this->setErrorPageURL('/error.php');
}
public function setErrorPageURL($errorPageURL);
public function handle() {
...
$this->ErrorLogger->log($errorNumber, $errorMessage, $errorFile, $errorLine);
header("Location: $this->errorPageURL");
}
}
class ErrorLogger {
abstract protected function log($errorNumber, $errorMessage, $errorFile, $errorLine);
// Overridable.
protected function getExtraLogMessage() {
return null;
}
}
class EmailerErrorLogger extends ErrorLogger {
public function __construct($toEmailAddress, $fromEmailAddress) {
...
}
protected function log($errorNumber, $errorMessage, $errorFile, $errorLine) {
// collect error information and various pieces of information
mail(...);
}
}
register_shutdown_function(array(new ErrorHandler(new EmailerErrorLogger()), 'handle'));