Handling exception information using the Observer pattern, observer mode
The capture of exception information is of great significance to the programming test, which combines the observer pattern to explore how to deal with exception information.
Regarding the observer pattern, if has not been contacted, the blog Park has many excellent Bo friends to do the detailed explanation. I think that the so-called observer pattern, must have two important components: a Subject object, multiple observers. When used, we can insert the observer like a plug into the socket of the subject object and use the Subject object to complete the function.
Since the observer wants to be a plug, it must have a uniform caliber to plug into the same socket, thus defining an interface first, exception_observer.php:
/* * * Definitions of specifications */ Interface exception_observer{ publicfunction$e);} ? >
In relation to many observers, we should first focus on the only Subject object, observer_exception.php:
PhpclassObserver_exceptionextends Exception{ Public Static $_observers=Array(); Public Static functionAttach (Exception_observer$observer) { self::$_observers[]=$observer; } Public function__construct ($message=NULL,$code=0) {Parent:: __construct ($message,$code); $this-notify (); } Public functionNotify () {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.
It is important to note that $observer->update ($this) , $this usage, many beginners will feel "original $this You can use it that way. "
A small problem: $_observers is not a static variable? We answer this question later.
Define two observers and in principle implement the functions defined by the interface.
email_exception_observer.php:
classEmailing_exception_observerImplementsexception_observer{protected $_email= "Huanggbxjp@sohu.com"; function__construct ($email=NULL) { if($email!==NULL&&filter_var ($email,filter_validate_email)) { $this->_email=$email; } } Public functionUpdate (observer_exception$e){ $message= "Time".Date("Y-m-d h:i:s").Php_eol; $message. = "Info".$e->getmessage ().Php_eol; $message. = "Tracking Information".$e->gettraceasstring ().Php_eol; $message. = "File".$e->getfile ().Php_eol; $message. = "line number".$e->getline ().Php_eol; Error_log($message, 1,$this-_email); }}
logging_exception_observer.php:
PhpclassLogging_exception_observerImplementsexception_observer{protected $_filename= "F:/logexception.log"; function__construct ($filename=NULL) { if($filename!==NULL&&is_string($filename)) { $thvis->_filename=$filename; } } Public functionUpdate (observer_exception$e){ $message= "Time".Date("Y-m-d h:i:s").Php_eol; $message. = "Info".$e->getmessage ().Php_eol; $message. = "Tracking Information".$e->gettraceasstring ().Php_eol; $message. = "File".$e->getfile ().Php_eol; $message. = "line number".$e->getline ().Php_eol; Error_log($message, 3,$this-_filename); }}
After designing all of the principal objects and plugins, we do a little testing:
Phprequire' Exception_observer.php ';require' Observer_exception.php ';require' Logging_exception_observer.php ';require' Emailing_exception_observer.php '; Observer_exception:: Attach (Newlogging_exception_observer ());classMyExceptionextendsobserver_exception{ Public functionTest () {Echo' This is a test '; } Public functiontest1 () {Echo"I am a custom method to handle this exception"; }}Try { Throw NewMyException ("An exception occurred, record"); } Catch(myexception$e) { Echo $e-GetMessage (); Echo"
"; }?>
This example first loads the observer and then makes other operations. Back to the question raised above, can $_observers be a static variable? The answer is no. If $_observers is not a static variable, loading the observer's behavior has no effect on subsequent operations. Static lets all instance members share a variable. Even class inheritance works equally well. If you are interested, you can continue to explore the magical effects of static.
This example shows that the output is the same as in the general case, but the difference is that the corresponding log has been generated under the custom file. Although the last function is simple enough, many people can even do it in less code, but in the case of more complex systems, the observer pattern brings us great convenience.
http://www.bkjia.com/PHPjc/1048766.html www.bkjia.com true http://www.bkjia.com/PHPjc/1048766.html techarticle using the Observer pattern to handle exception information, the capture of the observer pattern Anomaly information is of great significance to the programming test, which combines the observer pattern to explore how to handle the exception information. ...