HomePHP 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.
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.