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

Logging errors when something goes wrong - 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

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
 

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: