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

The error logging system in action - 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

If you're like me, then you'll think that the best way of understanding how the  error logging system works is by developing a comprehensive example which uses all the classes in conjunction.

Therefore, below you'll see a simple script I coded that shows the respective behaviors followed by all the classes defined over the previous sections, whether or not a concrete error logger has been set. Please look at this handy group of examples:

try{
    // instantiate 'ErrorLogger' object
    $errorLogger=new ErrorLogger();
    // validate email without providing an error logger
    $errorLogger->getErrorLogger();
    /* 
    displays the following: 
    No error logger has been set!
    */
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}
try{
    // instantiate 'ErrorLogger' object
    $errorLogger=new ErrorLogger();
    // instantiate 'FileErrorLogger'
    $fileErrorLogger=new FileErrorLogger($errorLogger);
    // instantiate 'EmailValidator' object
    //$emailValidator=new EmailValidator('username@domain.com',$fileErrorLogger);
    //$emailValidator->validate();
    /*
    displays the following:
    No error logger has been set!
    */
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}
try{
    // instantiate 'ErrorLogger' object
    $errorLogger=new ErrorLogger();
    // provide an error logger
    $fileErrorLogger=new FileErrorLogger($errorLogger);
    $fileErrorLogger->setErrorLogger('File Error Logger');
    $emailValidator=new EmailValidator('username@domain.com',$fileErrorLogger);
    $emailValidator->validate();
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

As shown above, the first two examples demonstrate clearly how the chain of responsibility is moved up toward the base "ErrorLogger" class when an appropriate error logger isn't provided to the scripts. This condition is reflected by the outputs generated by each script, since they complain loudly about this condition by displaying the following message:

No error logger has been set!

Finally, the last example illustrates a concrete case where a "FileErrorLogger" object is injected straight into the respective "EmailValidator" class. As you'll realize, this fact obviously allows the correct implementation of its "validate()" method. As usual, I suggest you to try modifying the previous examples and see what happens in each case.

Final thoughts

I'm finished now. In this three-part series, I introduced you to the basics of how to implement the chain of responsibility pattern with PHP 5. I hope that all the examples that were shown in this set of articles will help you start applying this schema in your own PHP applications. See you in the next PHP tutorial! 



 
 
>>> 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: