Should I be using delegation here?

I currently have two class hierarchies, one for handling errors and one for registering them.

I'm curious

1) is it advisable to delegate error handling to a class separate from the error handler

AND

2) do i have a good design.

Here's 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'));

      

0


a source to share


1 answer


I think this structure makes sense, because your error handler needs to do some other things than just log the error.



Journalists themselves could implement the chain of responsibility

+1


a source







All Articles