Handle exception information in observer mode, observer Mode

Source: Internet
Author: User

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:

<? Php/*** defined specification */interface Exception_Observer {public function update (Observer_Exception $ e) ;}?>

Compared with many observers, we should first focus on the unique theme object, Observer_Exception.php:

<?phpclass Observer_exception extends Exception{    public static $_observers=array();    public static function attach(Exception_Observer $observer){        self::$_observers[]=$observer;    }     public function __construct($message=null,$code=0){        parent::__construct($message,$code);        $this->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:

<? Php class Logging_Exception_Observer implements Exception_Observer {protected $ _ filename = "F:/logException. log"; function _ construct ($ filename = null) {if ($ filename! = Null & is_string ($ filename) {$ thvis-> _ 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:

<? Php require 'exception _ Observer. php '; require' Observer _ Exception. php '; require 'logging _ Exception_Observer.php'; require 'emailing _ Exception_Observer.php '; Observer_Exception: attach (new handler (); class MyException extends Observer_Exception {public function test () {echo 'this is a test';} public function test1 () {echo "I Am a custom method to handle this exception ";}} try {throw new MyException ("exception occurred, record "); } Catch (MyException $ e) {echo $ e-> 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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.