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