Home arrow PHP arrow Page 3 - Building an Error Logger with the Chain of Responsibility Pattern in PHP 5

Logging specific errors at a lower level - PHP

If you’re one of those PHP developers that wants to expand your background in pattern-based programming, then this article may suit your needs. Welcome to the second part of the series “Understanding the Chain of Responsibility Between PHP Objects.” Comprised of three installments, this series goes through the basics of building a chain of responsibility across several PHP objects, and teaches you how to apply this pattern by using copious code samples.

TABLE OF CONTENTS:
  1. Building an Error Logger with the Chain of Responsibility Pattern in PHP 5
  2. Setting up the foundations of an error logger
  3. Logging specific errors at a lower level
  4. Logging email-related errors
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
November 06, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: