PHP
  Home arrow PHP arrow Page 3 - Building an Error Logger with the Chain of Responsibility Pattern in PHP 5
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

Building an Error Logger with the Chain of Responsibility Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2006-11-06


    Table of Contents:
  • Building an Error Logger with the Chain of Responsibility Pattern in PHP 5
  • Setting up the foundations of an error logger
  • Logging specific errors at a lower level
  • Logging email-related errors

  • 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


    Building an Error Logger with the Chain of Responsibility Pattern in PHP 5 - Logging specific errors at a lower level
    ( Page 3 of 4 )

    As was explained in the previous section, to construct a new error logger that will be focused only on registering specific events, it's necessary to derive another class from the highly generic "ErrorLogger."

    As you may guess, this new class will be responsible for handling all the errors occurring only inside its scope. In addition, in accordance with the logic implemented by the corresponding chain of responsibility, the class will delegate program control to its nearest parent, in this case represented by the "ErrorLogger" class, if a particular error can't be handled properly.

    More specifically, since I want this new class to be focused on registering all the errors that happen when validating an email address, I called it "MailErrorLogger." Its definition is as follows:

    // define 'MailErrorLogger' class
    class MailErrorLogger 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!='Email Error Logger'){ 
                throw new Exception('Invalid error logger'); 
            } 
            $this->errorLogger=$errorLogger; 
        } 
        // log error by email to system administrator 
        public function logError(){ 
            mail('sysadmin@domain.com','Message from: '.$this-
    >getErrorLogger(),'An error occurred when validating email
    address!');
        }
    }

    As shown above, the "MailErrorLogger" class has been provided with a basic ability, handy for logging all the eventual errors raised when checking an email address. In particular, the "logError()" method performs the error logging process by notifying the system administrator when this type of failure occurs, something clearly demonstrated by its corresponding definition.

    Also, it should be noticed how the "getErrorLogger()" method implements, in an efficient way, the corresponding chain of responsibility: if there's a situation when an appropriate error logger can't be retrieved, this task is simply transferred to its parent, that is the pertinent "ErrorLogger" class. Now do you understand how the chain of responsibility really works here? I'm sure you do!

    The remaining class methods speak for themselves, therefore I'll spend no time explaining how they work. Instead, once the previous "MailErrorLogger" class has been correctly defined, I'd like to show you a concrete example where this error logging class can be used with fairly good results.

    That being said, over the next few lines I'll be demonstrating how the "MailErrorLogger" class can be utilized in conjunction with an email checking system to log errors when a specified email address fails to pass the verification process.

    Want to find out how this will be done? Go ahead and read the following section.



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