PHP
  Home arrow PHP arrow Page 3 - Expanding an Error Logger with the Chain of Responsibility Pattern
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Expanding an Error Logger with the Chain of Responsibility Pattern
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2006-11-13


    Table of Contents:
  • Expanding an Error Logger with the Chain of Responsibility Pattern
  • The error logging system's responsibility chain
  • Expanding the basic error logging system
  • Logging errors when something goes wrong
  • The error logging system in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Expanding an Error Logger with the Chain of Responsibility Pattern - Expanding the basic error logging system
    ( Page 3 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT