Exception information is processed in observer mode. The observer mode is used to process exception information. the capture of exception information in the Observer Mode is of great significance for programming testing. here we use the observer mode to explore how to handle exception information. Handle exception information in Observer Mode, Observer Mode
Capturing exception information is of great significance for programming testing. here we will explore how to handle exception information in conjunction with the Observer mode.
If you have not touched the observer model, many excellent bloggers in the blog Park have explained it in detail. The author thinks that the so-called observer mode must have two important components: one topic object and multiple observers. In use, we can plug the observer into the theme object socket like a plug, and use the theme object to complete the corresponding functions.
Since the observer must be used as a plug, a uniform caliber must be provided before it can be inserted into the same socket, so first define an interface, Exception_Observer.php:
Compared with many observers, we should first focus on the unique theme object, Observer_Exception.php:
notify(); } public function notify(){ foreach (self::$_observers as $observer) { $observer->update($this); } }}
We can clearly see that the static variable $ _ observers is used to place the inserted observer, and notify () is used to notify all observer objects.
Pay attention to the usage of $ observer-> update ($ this); $ this in it. many beginners will feel that "the original $ this can also be used ".
A small problem: $ _ observers is not a static variable, right? We will answer this question later.
Define two observers. in principle, the functions defined by the interface are implemented.
Email_Exception_Observer.php:
Class Emailing_Exception_Observer implements Exception_Observer {protected $ _ email = "huanggbxjp@sohu.com"; function _ construct ($ email = null) {if ($ email! = Null & filter_var ($ email, FILTER_VALIDATE_EMAIL) {$ this-> _ email = $ email ;}} public function update (Observer_Exception $ e) {$ message = "time ". date ("Y-m-d H: I: s "). PHP_EOL; $ message. = "information ". $ e-> getMessage (). PHP_EOL; $ message. = "tracing information ". $ e-> getTraceAsString (). PHP_EOL; $ message. = "file ". $ e-> getFile (). PHP_EOL; $ message. = "row number ". $ e-> getLine (). PHP_EOL; error_log ($ message, 1, $ this-> _ email );}}
Logging_Exception_Observer.php:
_ Filename = $ filename;} public function update (Observer_Exception $ e) {$ message = "time ". date ("Y-m-d H: I: s "). PHP_EOL; $ message. = "information ". $ e-> getMessage (). PHP_EOL; $ message. = "tracing information ". $ e-> getTraceAsString (). PHP_EOL; $ message. = "file ". $ e-> getFile (). PHP_EOL; $ message. = "row number ". $ e-> getLine (). PHP_EOL; error_log ($ message, 3, $ this-> _ filename );}}
After all the subject objects and plug-ins are designed, let's make a small test:
GetMessage (); echo"
";}?>
This instance first loads the Observer and then performs other operations. Back to the question above, can $ _ observers be static variables? The answer is No. If $ _ observers is not a static variable, loading the observer does not affect subsequent operations. Static allows all instance members to share a variable. Even the class inheritance is also valid. If you are interested, continue to explore the magic of static.
This example shows that the output is similar to the general situation, but the difference is that the corresponding log has been generated under the custom file. Although the final implementation functions are simple, many people can even use less code to implement them in a simpler way. However, in the case of more complex systems, the Observer Mode brings us great convenience.
Capturing the observer exception information is of great significance for programming testing. here we will explore how to handle the exception information in conjunction with the Observer mode ....