Home arrow PHP arrow Page 2 - Expanding an Error Logger with the Chain of Responsibility Pattern

The error logging system's responsibility chain - PHP

Do you want to know how to build an error logging system using the chain of responsibility schema with PHP? If your answer is a loud yes, then in this group of articles you’ll find what you’ve been looking for! Welcome to the concluding part of the series “Understanding the chain of responsibility between PHP objects.” This set of three tutorials shows you how to define a specific chain of responsibility that involves several PHP objects, and applies this concept to creating an expandable error logging mechanism.

TABLE OF CONTENTS:
  1. Expanding an Error Logger with the Chain of Responsibility Pattern
  2. The error logging system's responsibility chain
  3. Expanding the basic error logging system
  4. Logging errors when something goes wrong
  5. The error logging system in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 13, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
   

As I explained earlier, this final article will be focused on extending the functionality of the error logging system that I created in the previous part by applying a proper chain of responsibility schema.

Since this sounds pretty simple, I'll start listing the base error logging classes, which sit on top of the mentioned responsibility chain. Here is the signature of the two classes that I referenced above:

// define abstract 'AbstractErrorLogger' class on top of the
responsibility chain
abstract class AbstractErrorLogger{
    abstract public function setErrorLogger($errorLogger);
    abstract public function getErrorLogger();
    abstract public function logError();
}
// define concrete 'ErrorLogger' class
class ErrorLogger extends AbstractErrorLogger{
    private $errorLogger;
    public function __construct(){
        $this->errorLogger=NULL;
    }
    // get error logger
    public function getErrorLogger(){
        if($this->errorLogger==NULL){
            throw new Exception('No error logger has been set!');
        }
        return $this->errorLogger;
    }
    // set error logger
    public function setErrorLogger($errorLogger){
        $this->errorLogger=$errorLogger;
    }
    public function logError(){}
}

As you'll possibly remember, the above two error logging classes establish the top of the corresponding responsibility chain, from which all the eventual subclasses will be derived. Basically, the first abstract class defines the generic model of any possible error logger, while the second one implements concretely most of the abstract methods declared previously.

So far, the respective definitions of the above base classes aren't difficult to understand. However, it's worthwhile to stress here the way that the "getErrorLogger()" method works. As you can see, this method sets up the top of the responsibility chain when handling an error, because if the appropriate error logger  has not been defined (represented by the condition $this->errorLogger==NULL), then it throws an exception and program execution will be eventually halted.

From this point on, all the sub classes created from the previous "ErrorLogger" class will stick to the following logic: if a particular error condition can be properly handled by the class in question, then the error will be automatically transferred to its nearest parent, which as you saw a few lines above, will trigger an exception. That's what I call a well-defined chain of responsibility between a few PHP classes!

All right, after explaining the fairly relevant role played by the two pair of error classes created before, it's time to learn how the core error logging system can be expanded to register more specific failures.

In this particular case, I'll simply derive a child class from the parent "ErrorLogger," which will be provided with the capacity to register specific errors to the system's error logger.

In addition, based upon the correct application of the chain of responsibility schema, this new class will delegate the handling of a specific error to its parent, when this failure can be processed at the current level.

To learn more about how this new error logging class will be created, please keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: