【发布时间】:2009-05-12 17:34:22
【问题描述】:
我目前有两个类层次结构:一个用于处理错误,另一个用于记录错误。
我想知道
1) 将错误处理委托给一个独立于错误处理程序的类是否有利
还有
2) 我所拥有的是否是一个好的设计。
这是我所拥有的:
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'));
【问题讨论】:
标签: php logging error-handling