PHP
  Home arrow PHP arrow Page 4 - 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 - Logging errors when something goes wrong
    ( Page 4 of 5 )

    To illustrate the functionality of the error logging system that I created earlier in this article, I'll define a sample email checking class. It will work with the file error logger mechanism that you saw before, to implement in an efficient way the corresponding chain of responsibility model.

    That being said, here is the respective signature of this new email verification class:

    // define 'EmailValidator' class
    class EmailValidator{
        private $email;
        private $fileErrorLogger;
        public function __construct($email,ErrorLogger
    $fileErrorLogger){
            $this->fileErrorLogger=$fileErrorLogger;
            if(!preg_match("/^.+@.+$/",$email)){
                $this->fileErrorLogger->logError();
            }
            $this->email=$email;
        }
        // validate email
        public function validate(){
            if(!$this->windnsrr(array_pop(explode("@",$this-
    >email)))){
                $this->fileErrorLogger->logError();
            }
        }
        // check for MX records in the DNS (Windows-based systems)
        private function windnsrr($hostName,$recType=''){
            if(!$hostName){
                if($recType=='')$recType="MX";
                exec("nslookup -type=$recType $hostName",$result);
                foreach($result as $line){
                    if(preg_match("/^$hostName/",$line)){
                        return true;
                    }
                }
                return false;
            }
            return false;
        }
    }

    As you can see, the logic followed by the above "EmailValidator" class is fairly comprehensive, not only with reference to its limited functionality, but because this class was shown in the previous article of the series too.

    In this case, the class in question is capable of validating a given email address on any Window-based system by mean of its "validate()" method, and not surprisingly when the email fails to pass the verification process, the failure is logged via the intuitive "FileErrorLogger" class.

    Of course, aside from the features that I explained previously, the only thing worth mentioning here is the appropriate delegation of responsibility that takes place when the error condition can't be handled by the file error logging class, which, as you learned before, results in moving the program's flow to its respective parent.

    At this stage, I'm sure that you understood how the "EmailValidator" class does its business, as well as how the chain of responsibility works across all the previously defined error loggers. Therefore it's time to move on and see a concrete example where all the classes will be put to work together.

    Want to see how this hands-on example will be developed? Go ahead and read the next few lines.



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