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

Expanding the basic error logging system - 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 expressed in the previous section, expanding the basic error logging mechanism that was shown before consists essentially of deriving a brand-new sub class from the base "ErrorLogger." This creates a mechanism that will be capable of registering specific errors to the system's error logger.

Based on this premise, here is the signature of this concrete child class, which I called "FileErrorLogger." Please take a look at its definition:

// define 'FileErrorLogger' class
class FileErrorLogger extends ErrorLogger{
    private $errorLogger;
    private $parentErrorLogger;
    public function __construct(ErrorLogger $parentErrorLogger){
        $this->errorLogger=NULL;
        $this->parentErrorLogger=$parentErrorLogger;
    }
    // get error logger
    public function getErrorLogger(){
        if($this->errorLogger==NULL){
            // call parent object in chain of responsibility
            return $this->parentErrorLogger->getErrorLogger();
        }
        else{
            return $this->errorLogger;
        }
    }
    // set error logger
    public function setErrorLogger($errorLogger){
        if($errorLogger!='File Error Logger'){
            throw new Exception('Invalid error logger');
        }
        $this->errorLogger=$errorLogger;
    }
    // log error to file
    public function logError(){
        error_log('Message from :'.$this->getErrorLogger(),0);
    }
}

If you take an in-depth look at the above error logger class, you'll understand quickly how it does its thing. In simple terms, the "FileErrorLogger" class has been provided with the ability to register specific errors to the system's error logger via the implementation of its respective "logError()" method. Really simple, isn't it?

Nevertheless, I actually want you to focus your attention on the definition of another handy method: "getErrorLogger()." It shows in a nutshell how the chain of responsibility model works. As you can see, the aforementioned method reveals the power of this schema by transferring the program's flow to its corresponding parent when a specific error condition can't be properly handled at that stage. Obviously, this condition is represented by the following expression:

if($this->errorLogger==NULL){
    // call parent object in chain of responsibility
    return $this->parentErrorLogger->getErrorLogger();
}
else{
   return $this->errorLogger;

In this situation, when a concrete failure can't be appropriately processed by the pertinent "FileErrorLogger" class, the error condition will be delegated to its parent, which has been previously passed as an input argument via the corresponding constructor. At this point, are you starting to grasp the logic behind the chain of responsibility pattern? I hope you are!

So far, the file error logging class that I defined a few lines above will hopefully be more than enough to demonstrate how to create a responsibility chain between some PHP classes. However, I must say there's still a missing link inside this chain. I've not yet built a sample class capable of using the capabilities embedded into the previous "FileErrorLogger." Therefore, the question is: how can this be achieved?

Fortunately, creating a new class that utilizes the handy features provided by the prior "FileErrorLogger" class is indeed a no-brainer process, certainly a fact that I'm going to demonstrate over the course of the following section. Therefore, I suggest you click on the link below and 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: