异常信息的捕获对编程测试有着重要的意义,这里结合观察者模式,探索如何处理异常信息。
关于观察者模式,如果还没有接触过的话,博客园有很多优秀的博友做了详细的 解释。笔者觉得,所谓观察者模式,必须有两个重要组成部分:一个主题对象,多个观察者。在使用的时候,我们可以将观察者像插头一样插到主题对象这个插座上,利用主题对象完成相应功能。
既然观察者要作为插头,必须要有一个统一的口径才能插到相同的插座上,因而先定义一个接口,Exception_Observer.php:
<?<span>php </span><span>/*</span><span>* * 定义的规范 </span><span>*/</span> <span>interface</span><span> Exception_Observer{ </span><span>public</span> <span>function</span> update(Observer_Exception <span>$e</span><span>); } </span>?>
相对于众多观察者,我们首先应该关注唯一的主题对象,Observer_Exception.php:
<?<span>php </span><span>class</span> Observer_exception <span>extends</span> <span>Exception</span><span>{ </span><span>public</span> <span>static</span> <span>$_observers</span>=<span>array</span><span>(); </span><span>public</span> <span>static</span> <span>function</span> attach(Exception_Observer <span>$observer</span><span>){ self</span>::<span>$_observers</span>[]=<span>$observer</span><span>; } </span><span>public</span> <span>function</span> __construct(<span>$message</span>=<span>null</span>,<span>$code</span>=0<span>){ parent</span>::__construct(<span>$message</span>,<span>$code</span><span>); </span><span>$this</span>-><span>notify(); } </span><span>public</span> <span>function</span><span> notify(){ </span><span>foreach</span> (self::<span>$_observers</span> <span>as</span> <span>$observer</span><span>) { </span><span>$observer</span>->update(<span>$this</span><span>); } } }</span>
我们可以清楚地看到,静态变量$_observers用来放置插入的观察者,notify()用来通知所有观察者对象。
这里需要注意 $observer->update($this); 里面 $this 的用法,很多初学者会感到“原来 $this 也可以这么用啊”。
一个小问题: $_observers 不是静态变量可不可以? 这个问题我们后面回答。
定义两个观察者,原则上实现接口所定义的功能。
Email_Exception_Observer.php:
<span>class</span> Emailing_Exception_Observer <span>implements</span><span> Exception_Observer{ </span><span>protected</span> <span>$_email</span>="huanggbxjp@sohu.com"<span>; </span><span>function</span> __construct(<span>$email</span>=<span>null</span><span>) { </span><span>if</span> (<span>$email</span>!==<span>null</span>&&filter_var(<span>$email</span>,<span>FILTER_VALIDATE_EMAIL)) { </span><span>$this</span>->_email=<span>$email</span><span>; } } </span><span>public</span> <span>function</span> update(Observer_Exception <span>$e</span><span>){ </span><span>$message</span>="时间".<span>date</span>("Y-m-d H:i:s").<span>PHP_EOL</span><span>; </span><span>$message</span>.="信息".<span>$e</span>->getMessage().<span>PHP_EOL</span><span>; </span><span>$message</span>.="追踪信息".<span>$e</span>->getTraceAsString().<span>PHP_EOL</span><span>; </span><span>$message</span>.="文件".<span>$e</span>->getFile().<span>PHP_EOL</span><span>; </span><span>$message</span>.="行号".<span>$e</span>->getLine().<span>PHP_EOL</span><span>; </span><span>error_log</span>(<span>$message</span>,1,<span>$this</span>-><span>_email); } }</span>
Logging_Exception_Observer.php:
<?<span>php </span> <span>class</span> Logging_Exception_Observer <span>implements</span><span> Exception_Observer { </span><span>protected</span> <span>$_filename</span>="F:/logException.log"<span>; </span><span>function</span> __construct(<span>$filename</span>=<span>null</span><span>) { </span><span>if</span> (<span>$filename</span>!==<span>null</span>&&<span>is_string</span>(<span>$filename</span><span>)) { </span><span>$thvis</span>->_filename=<span>$filename</span><span>; } } </span><span>public</span> <span>function</span> update(Observer_Exception <span>$e</span><span>){ </span><span>$message</span>="时间".<span>date</span>("Y-m-d H:i:s").<span>PHP_EOL</span><span>; </span><span>$message</span>.="信息".<span>$e</span>->getMessage().<span>PHP_EOL</span><span>; </span><span>$message</span>.="追踪信息".<span>$e</span>->getTraceAsString().<span>PHP_EOL</span><span>; </span><span>$message</span>.="文件".<span>$e</span>->getFile().<span>PHP_EOL</span><span>; </span><span>$message</span>.="行号".<span>$e</span>->getLine().<span>PHP_EOL</span><span>; </span><span>error_log</span>(<span>$message</span>,3,<span>$this</span>-><span>_filename); } }</span>
设计完所有该有的主体对象和插件,我们做个小小的测试:
<?<span>php </span><span>require</span> 'Exception_Observer.php'<span>; </span><span>require</span> 'Observer_Exception.php'<span>; </span><span>require</span> 'Logging_Exception_Observer.php'<span>; </span><span>require</span> 'Emailing_Exception_Observer.php'<span>; Observer_Exception</span>::attach(<span>new</span><span> Logging_Exception_Observer()); </span><span>class</span> MyException <span>extends</span><span> Observer_Exception{ </span><span>public</span> <span>function</span><span> test(){ </span><span>echo</span> 'this is a test'<span>; } </span><span>public</span> <span>function</span><span> test1(){ </span><span>echo</span> "我是自定义的方法处理这个异常"<span>; } } </span><span>try</span><span> { </span><span>throw</span> <span>new</span> MyException("出现异常,记录一下"<span>); } </span><span>catch</span> (MyException <span>$e</span><span>) { </span><span>echo</span> <span>$e</span>-><span>getMessage(); </span><span>echo</span> "<ht/>"<span>; } </span>?>
本实例首先先加载观察者,其后进行其他操作。回到上面提出的问题, $_observers 可以不是静态变量吗?答案是不可以。如果 $_observers 不是静态变量,加载观察者的行为对后续操作没有影响。static让所有实例成员共享某个变量。即便类继承也同样有效。有兴趣的可以继续探索下static的神奇作用吧。
本例显示输出与一般情况无异,但不同的是已在自定义的文件下生成了相应的日志。虽然最后实现的功能再简单不过,很多人甚至可以用更少的代码更简单的方法实现,但是,在实现更加复杂系统的情况下,观察者模式给我们带来很大方便。