PHP
  Home arrow PHP arrow Page 2 - 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 - The error logging system's responsibility chain
    ( Page 2 of 5 )

       

    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
     

       

    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 1 Hosted by Hostway
    Stay green...Green IT