Home > Backend Development > PHP Tutorial > 使用观察者模式处理错误信息

使用观察者模式处理错误信息

WBOY
Release: 2016-06-13 12:23:44
Original
962 people have browsed it

使用观察者模式处理异常信息

  异常信息的捕获对编程测试有着重要的意义,这里结合观察者模式,探索如何处理异常信息。

  关于观察者模式,如果还没有接触过的话,博客园有很多优秀的博友做了详细的 解释。笔者觉得,所谓观察者模式,必须有两个重要组成部分:一个主题对象,多个观察者。在使用的时候,我们可以将观察者像插头一样插到主题对象这个插座上,利用主题对象完成相应功能。

  既然观察者要作为插头,必须要有一个统一的口径才能插到相同的插座上,因而先定义一个接口,Exception_Observer.php:

<span style="color: #000000;">php </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 定义的规范  </span><span style="color: #008000;">*/</span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> Exception_Observer{    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> update(Observer_Exception <span style="color: #800080;">$e</span><span style="color: #000000;">);}  </span>?>
Copy after login

  相对于众多观察者,我们首先应该关注唯一的主题对象,Observer_Exception.php:

<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span> Observer_exception <span style="color: #0000ff;">extends</span> <span style="color: #0000ff;">Exception</span><span style="color: #000000;">{    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$_observers</span>=<span style="color: #0000ff;">array</span><span style="color: #000000;">();    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> attach(Exception_Observer <span style="color: #800080;">$observer</span><span style="color: #000000;">){        self</span>::<span style="color: #800080;">$_observers</span>[]=<span style="color: #800080;">$observer</span><span style="color: #000000;">;    }     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$message</span>=<span style="color: #0000ff;">null</span>,<span style="color: #800080;">$code</span>=0<span style="color: #000000;">){        parent</span>::__construct(<span style="color: #800080;">$message</span>,<span style="color: #800080;">$code</span><span style="color: #000000;">);        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">notify();    }    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> notify(){        </span><span style="color: #0000ff;">foreach</span> (self::<span style="color: #800080;">$_observers</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$observer</span><span style="color: #000000;">) {            </span><span style="color: #800080;">$observer</span>->update(<span style="color: #800080;">$this</span><span style="color: #000000;">);        }    }}</span>
Copy after login

  我们可以清楚地看到,静态变量$_observers用来放置插入的观察者,notify()用来通知所有观察者对象。

  这里需要注意 $observer->update($this); 里面 $this 的用法,很多初学者会感到“原来 $this 也可以这么用啊”。

  一个小问题: $_observers 不是静态变量可不可以? 这个问题我们后面回答。

  定义两个观察者,原则上实现接口所定义的功能。

  Email_Exception_Observer.php:

<span style="color: #0000ff;">class</span> Emailing_Exception_Observer <span style="color: #0000ff;">implements</span><span style="color: #000000;"> Exception_Observer{    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_email</span>="[email protected]"<span style="color: #000000;">;    </span><span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$email</span>=<span style="color: #0000ff;">null</span><span style="color: #000000;">)    {        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$email</span>!==<span style="color: #0000ff;">null</span>&&filter_var(<span style="color: #800080;">$email</span>,<span style="color: #000000;">FILTER_VALIDATE_EMAIL)) {            </span><span style="color: #800080;">$this</span>->_email=<span style="color: #800080;">$email</span><span style="color: #000000;">;        }    }    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> update(Observer_Exception <span style="color: #800080;">$e</span><span style="color: #000000;">){        </span><span style="color: #800080;">$message</span>="时间".<span style="color: #008080;">date</span>("Y-m-d H:i:s").<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="信息".<span style="color: #800080;">$e</span>->getMessage().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="追踪信息".<span style="color: #800080;">$e</span>->getTraceAsString().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="文件".<span style="color: #800080;">$e</span>->getFile().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="行号".<span style="color: #800080;">$e</span>->getLine().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #008080;">error_log</span>(<span style="color: #800080;">$message</span>,1,<span style="color: #800080;">$this</span>-><span style="color: #000000;">_email);    }}</span>
Copy after login

  Logging_Exception_Observer.php:

<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span> Logging_Exception_Observer <span style="color: #0000ff;">implements</span><span style="color: #000000;"> Exception_Observer{    </span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_filename</span>="F:/logException.log"<span style="color: #000000;">;    </span><span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$filename</span>=<span style="color: #0000ff;">null</span><span style="color: #000000;">)    {        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$filename</span>!==<span style="color: #0000ff;">null</span>&&<span style="color: #008080;">is_string</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">)) {            </span><span style="color: #800080;">$thvis</span>->_filename=<span style="color: #800080;">$filename</span><span style="color: #000000;">;        }    }    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> update(Observer_Exception <span style="color: #800080;">$e</span><span style="color: #000000;">){        </span><span style="color: #800080;">$message</span>="时间".<span style="color: #008080;">date</span>("Y-m-d H:i:s").<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="信息".<span style="color: #800080;">$e</span>->getMessage().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="追踪信息".<span style="color: #800080;">$e</span>->getTraceAsString().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="文件".<span style="color: #800080;">$e</span>->getFile().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #800080;">$message</span>.="行号".<span style="color: #800080;">$e</span>->getLine().<span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;        </span><span style="color: #008080;">error_log</span>(<span style="color: #800080;">$message</span>,3,<span style="color: #800080;">$this</span>-><span style="color: #000000;">_filename);    }}</span>
Copy after login

    设计完所有该有的主体对象和插件,我们做个小小的测试:

<span style="color: #000000;">php </span><span style="color: #0000ff;">require</span> 'Exception_Observer.php'<span style="color: #000000;">;</span><span style="color: #0000ff;">require</span> 'Observer_Exception.php'<span style="color: #000000;">;</span><span style="color: #0000ff;">require</span> 'Logging_Exception_Observer.php'<span style="color: #000000;">;</span><span style="color: #0000ff;">require</span> 'Emailing_Exception_Observer.php'<span style="color: #000000;">;Observer_Exception</span>::attach(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Logging_Exception_Observer());</span><span style="color: #0000ff;">class</span> MyException <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Observer_Exception{    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test(){        </span><span style="color: #0000ff;">echo</span> 'this is  a test'<span style="color: #000000;">;    }    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test1(){        </span><span style="color: #0000ff;">echo</span> "我是自定义的方法处理这个异常"<span style="color: #000000;">;    }}</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {    </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> MyException("出现异常,记录一下"<span style="color: #000000;">);    } </span><span style="color: #0000ff;">catch</span> (MyException <span style="color: #800080;">$e</span><span style="color: #000000;">) {    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage();    </span><span style="color: #0000ff;">echo</span> "<ht></ht>"<span style="color: #000000;">;    }</span>?>
Copy after login

  本实例首先先加载观察者,其后进行其他操作。回到上面提出的问题, $_observers 可以不是静态变量吗?答案是不可以。如果 $_observers 不是静态变量,加载观察者的行为对后续操作没有影响。static让所有实例成员共享某个变量。即便类继承也同样有效。有兴趣的可以继续探索下static的神奇作用吧。 

  本例显示输出与一般情况无异,但不同的是已在自定义的文件下生成了相应的日志。虽然最后实现的功能再简单不过,很多人甚至可以用更少的代码更简单的方法实现,但是,在实现更加复杂系统的情况下,观察者模式给我们带来很大方便。

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template