# (\*)观察者模式
## **观察者模式**
> 当一个对象状态发生改变的时候,依赖他的对象全部会收到通知,并且自动更新等等一连串的操作, 将这些一连串的操作(的逻辑代码)放到观察者列表里
> 场景:一个事件发生后,要执行一连串的更新操作,传统的编程方法,就是在事件的代码之后直接加入逻辑处理,当更新的逻辑增多之后,代码会变得难以维护。这种方式是耦合的,侵入式的。增加新的逻辑需要修改事件主体的代码
> 察者模式实现了低耦合,非侵入式的通知与更新机制
> 状态触发类的应用当你的程序要处理的对象有很多种状态,就可以考虑使用这个模式
> 如果想在框架任何一个地方里插入其他逻辑二无需大的改动,请用观察者模式,只需在需要的地方添加一段触发观察的代码就可以
## **业务场景:**
**窗体程序设计中的事件处理**-- 窗体中的所有组件都是“事件源”,也就是目标对象,而事件处理程序类的对象是具体观察者对象
**注册**-- 一般和活动挂钩如注册送金币和积分也可以送给推荐人
**下订单**--下订单就多了 活动是最起码的 其次 比如发短信发邮件存入日志
**插件** -- 比如许多框架的第三方插件就是这个原理
**订阅功能** --简单的是一些第三方的支付接入或者其他功能接入,他们都暴露一个订阅功能,你可以选择订阅,然后你就作为观察者,每次第三方有更新和通知的时候,所有订阅的人,都能收到更新通知了
## **模式的应用场景**
1. 对象间存在一对多关系,一个对象的状态发生改变会影响其他对象。
2. 当一个抽象模型有两个方面,其中一个方面依赖于另一方面时,可将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
## **模式的结构**
1. **抽象主题(Subject)角色**:也叫抽象目标类,它提供了一个用于**保存**观察者对象的聚集类和**增加**、**删除**观察者对象的方法,以及**通知**所有观察者的抽象方法。
2. **具体主题(Concrete Subject)角色**:也叫具体目标类,它实现抽象目标中的通知方法,当具体主题的内部状态发生改变时,通知所有注册过的观察者对象。
3. **抽象观察者(Observer)角色**:它是一个抽象类或接口,它包含了一个更新自己的抽象方法,当接到具体主题的更改通知时被调用。
4. **具体观察者(Concrete Observer)角色**:实现抽象观察者中定义的抽象方法,以便在得到目标的更改通知时更新自身的状态。

```
<pre class="calibre10">```
<span class="token">//抽象目标</span>
abstract class <span class="token4">Subject</span>
<span class="token3">{</span>
protected $_observers<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//增加观察者方法</span>
public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Observer $observer<span class="token3">)</span>
<span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>_observers<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$observer<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//删除观察者方法</span>
public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Observer $observer<span class="token3">)</span>
<span class="token3">{</span>
<span class="token4">foreach</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_observers as $index <span class="token1">=</span><span class="token1">></span> $observer<span class="token3">)</span> <span class="token3">{</span>
<span class="token5">if</span> <span class="token3">(</span>$observer <span class="token1">===</span> $observer<span class="token3">)</span> <span class="token3">{</span>
<span class="token4">array_splice</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_observers<span class="token3">,</span> $index<span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span>
<span class="token5">return</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//通知观察者方法</span>
public abstract <span class="token5">function</span> <span class="token4">notifyObserver</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//具体目标</span>
class <span class="token4">ConcreteSubject</span> extends <span class="token4">Subject</span>
<span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">notifyObserver</span><span class="token3">(</span><span class="token3">)</span>
<span class="token3">{</span>
<span class="token4">print_r</span><span class="token3">(</span><span class="token2">"具体目标发生改变...<br>"</span><span class="token3">)</span><span class="token3">;</span>
<span class="token4">print_r</span><span class="token3">(</span><span class="token2">"--------------<br>"</span><span class="token3">)</span><span class="token3">;</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_observers as $observer<span class="token3">)</span> <span class="token3">{</span>
$observer<span class="token1">-</span><span class="token1">></span><span class="token4">response</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//抽象观察者</span>
interface <span class="token4">Observer</span>
<span class="token3">{</span>
<span class="token5">function</span> <span class="token4">response</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//反应</span>
<span class="token3">}</span>
<span class="token">//具体观察者1</span>
class <span class="token4">ConcreteObserver1</span> implements <span class="token4">Observer</span>
<span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">response</span><span class="token3">(</span><span class="token3">)</span>
<span class="token3">{</span>
<span class="token4">print_r</span><span class="token3">(</span><span class="token2">"具体观察者1作出反应!"</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//具体观察者1</span>
class <span class="token4">ConcreteObserver2</span> implements <span class="token4">Observer</span>
<span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">response</span><span class="token3">(</span><span class="token3">)</span>
<span class="token3">{</span>
<span class="token4">print_r</span><span class="token3">(</span><span class="token2">"具体观察者2作出反应!"</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
class <span class="token4">ObserverPattern</span>
<span class="token3">{</span>
public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span>
<span class="token3">{</span>
$subject<span class="token1">=</span><span class="token5">new</span> <span class="token4">ConcreteSubject</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$subject<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">ConcreteObserver1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$subject<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">ConcreteObserver2</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$subject<span class="token1">-</span><span class="token1">></span><span class="token4">notifyObserver</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
ObserverPattern<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
```
```
例子:
```
<pre class="calibre10">```
<span class="token">//抽象主题角色</span>
interface <span class="token4">ISubject</span><span class="token3">{</span>
<span class="token5">function</span> <span class="token4">addObserver</span><span class="token3">(</span> $observer <span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">// 具体主题角色(被观察者) 用于添加、删除、保存、通知观察者</span>
class <span class="token4">UserList</span> implements <span class="token4">ISubject</span><span class="token3">{</span>
private $_observers <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">sendMsg</span><span class="token3">(</span> $name <span class="token3">)</span><span class="token3">{</span>
<span class="token4">foreach</span><span class="token3">(</span> $this<span class="token1">-</span><span class="token1">></span>_observers as $obs <span class="token3">)</span><span class="token3">{</span>
$obs<span class="token1">-</span><span class="token1">></span><span class="token4">onSendMsg</span><span class="token3">(</span> $this<span class="token3">,</span> $name <span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">addObserver</span><span class="token3">(</span> $observer <span class="token3">)</span><span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>_observers<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span> $observer<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">removeObserver</span><span class="token3">(</span>$observer_name<span class="token3">)</span> <span class="token3">{</span>
<span class="token4">foreach</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_observers as $index <span class="token1">=</span><span class="token1">></span> $observer<span class="token3">)</span> <span class="token3">{</span>
<span class="token5">if</span> <span class="token3">(</span>$observer<span class="token1">-</span><span class="token1">></span><span class="token4">getName</span><span class="token3">(</span><span class="token3">)</span> <span class="token1">===</span> $observer_name<span class="token3">)</span> <span class="token3">{</span>
<span class="token4">array_splice</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_observers<span class="token3">,</span> $index<span class="token3">,</span> <span class="token6">1</span><span class="token3">)</span><span class="token3">;</span>
<span class="token5">return</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//抽象观察者</span>
interface <span class="token4">IObserver</span><span class="token3">{</span>
<span class="token5">function</span> <span class="token4">onSendMsg</span><span class="token3">(</span> $sender<span class="token3">,</span> $args <span class="token3">)</span><span class="token3">;</span>
<span class="token5">function</span> <span class="token4">getName</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//具体观察者角色1</span>
class <span class="token4">UserListLogger</span> implements <span class="token4">IObserver</span><span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">onSendMsg</span><span class="token3">(</span> $sender<span class="token3">,</span> $args <span class="token3">)</span><span class="token3">{</span>
<span class="token4">echo</span><span class="token3">(</span> <span class="token2">"'$args' 以发送到 UserListLogger\n"</span> <span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">getName</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> static<span class="token3">:</span><span class="token3">:</span>class<span class="token3">;</span><span class="token">//同 return 'UserListLogger';</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//具体观察者角色2</span>
class <span class="token4">OtherObserver</span> implements <span class="token4">IObserver</span><span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">onSendMsg</span><span class="token3">(</span> $sender<span class="token3">,</span> $args <span class="token3">)</span><span class="token3">{</span>
<span class="token4">echo</span><span class="token3">(</span> <span class="token2">"'$args' 已发送到 OtherObserver\n"</span> <span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">getName</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> static<span class="token3">:</span><span class="token3">:</span>class<span class="token3">;</span><span class="token">//同 return 'OtherObserver';</span>
<span class="token3">}</span>
<span class="token3">}</span>
$ul <span class="token1">=</span> <span class="token5">new</span> <span class="token4">UserList</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//被观察者</span>
$ul<span class="token1">-</span><span class="token1">></span><span class="token4">addObserver</span><span class="token3">(</span> <span class="token5">new</span> <span class="token4">UserListLogger</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">)</span><span class="token3">;</span><span class="token">//增加观察者</span>
$ul<span class="token1">-</span><span class="token1">></span><span class="token4">addObserver</span><span class="token3">(</span> <span class="token5">new</span> <span class="token4">OtherObserver</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">)</span><span class="token3">;</span><span class="token">//增加观察者</span>
$ul<span class="token1">-</span><span class="token1">></span><span class="token4">sendMsg</span><span class="token3">(</span> <span class="token2">"Jack"</span> <span class="token3">)</span><span class="token3">;</span><span class="token">//发送消息到观察者</span>
echo <span class="token2">"<br>"</span><span class="token3">;</span>
$ul<span class="token1">-</span><span class="token1">></span><span class="token4">removeObserver</span><span class="token3">(</span><span class="token2">'UserListLogger'</span><span class="token3">)</span><span class="token3">;</span><span class="token">//移除观察者</span>
$ul<span class="token1">-</span><span class="token1">></span><span class="token4">sendMsg</span><span class="token3">(</span><span class="token2">"hello"</span><span class="token3">)</span><span class="token3">;</span><span class="token">//发送消息到观察者 </span>
```
```
## **例子**
php 内置了SplSubject与SplObserver接口可以更简便的实现观察者模式
> newspaper是被观察的对象,reader是观察者。当报纸发布消息, 每一个用户都会得到通知。这就是观察者模式的使用场景
```
<pre class="calibre10">```
class <span class="token4">Newspaper</span> implements <span class="token4">SplSubject</span> <span class="token3">{</span>
private $name<span class="token3">;</span>
private $observers<span class="token3">;</span>
private $content<span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span><span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>name <span class="token1">=</span> $name<span class="token3">;</span>
$this<span class="token1">-</span><span class="token1">></span>observers <span class="token1">=</span> <span class="token5">new</span> <span class="token4">SplObjectStorage</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//增加一个观察者 </span>
public <span class="token5">function</span> <span class="token4">attach</span><span class="token3">(</span>SplObserver $observer<span class="token3">)</span><span class="token3">{</span>
<span class="token">//向SplObjectStorage中添加一个对象</span>
<span class="token">//此处为SplObjectStorage对象存储类的attach方法而SplSubject 的</span>
$this<span class="token1">-</span><span class="token1">></span>observers<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$observer<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//移除一个观察者</span>
public <span class="token5">function</span> <span class="token4">detach</span><span class="token3">(</span>SplObserver $observer<span class="token3">)</span><span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>observers<span class="token1">-</span><span class="token1">></span><span class="token4">detach</span><span class="token3">(</span>$observer<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//通知所有观察者</span>
public <span class="token5">function</span> <span class="token4">notify</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>observers as $observer<span class="token3">)</span> <span class="token3">{</span>
$observer<span class="token1">-</span><span class="token1">></span><span class="token4">update</span><span class="token3">(</span>$this<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">getContent</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>content<span class="token3">.</span><span class="token2">"{$this->name}"</span><span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">breakOutNews</span><span class="token3">(</span>$content<span class="token3">)</span> <span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>content <span class="token1">=</span> $content<span class="token3">;</span>
$this<span class="token1">-</span><span class="token1">></span><span class="token4">notify</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token1"><</span><span class="token1">?</span>php
class <span class="token4">Reader</span> implements <span class="token4">SplObserver</span> <span class="token3">{</span>
private $name<span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span><span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>name <span class="token1">=</span> $name<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">update</span><span class="token3">(</span>SplSubject $subject<span class="token3">)</span> <span class="token3">{</span>
echo $this<span class="token1">-</span><span class="token1">></span>name<span class="token3">.</span><span class="token2">' is reading breakout news'</span><span class="token3">.</span>$subject<span class="token1">-</span><span class="token1">></span><span class="token4">getContent</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token1"><</span><span class="token1">?</span>php
include <span class="token2">"Newspaper.php"</span><span class="token3">;</span>
include <span class="token2">"Reader.php"</span><span class="token3">;</span>
class <span class="token4">WorkFlow</span> <span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">run</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
$newspaper <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Newspaper</span><span class="token3">(</span><span class="token2">'纽约时报'</span><span class="token3">)</span><span class="token3">;</span><span class="token">//SplSubject </span>
$allen <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Reader</span><span class="token3">(</span><span class="token2">"allen"</span><span class="token3">)</span><span class="token3">;</span><span class="token">//SplObserver</span>
$jimmy <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Reader</span><span class="token3">(</span><span class="token2">"jimmy"</span><span class="token3">)</span><span class="token3">;</span><span class="token">//SplObserver</span>
$tom <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Reader</span><span class="token3">(</span><span class="token2">"tom"</span><span class="token3">)</span><span class="token3">;</span><span class="token">//SplObserver</span>
$newspaper<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$allen<span class="token3">)</span><span class="token3">;</span><span class="token">//</span>
$newspaper<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$jimmy<span class="token3">)</span><span class="token3">;</span>
$newspaper<span class="token1">-</span><span class="token1">></span><span class="token4">attach</span><span class="token3">(</span>$tom<span class="token3">)</span><span class="token3">;</span>
$newspaper<span class="token1">-</span><span class="token1">></span><span class="token4">detach</span><span class="token3">(</span>$tom<span class="token3">)</span><span class="token3">;</span>
$newspaper<span class="token1">-</span><span class="token1">></span><span class="token4">breakOutNews</span><span class="token3">(</span><span class="token2">'USA BREAK DOWN'</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
$work <span class="token1">=</span> <span class="token5">new</span> <span class="token4">WorkFlow</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$work<span class="token1">-</span><span class="token1">></span><span class="token4">run</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
```
```
## **php中的钩子(hook插件机制)**
插件管理器类作为具体主题(被观察者)角色:也叫具体目标类,用于保存、添加、删除及通知观察者(目的是执行入口函数)
插件作为观察者,一旦被注册则调用插件的入口函数
> /index.php
```
<pre class="calibre10">```
<span class="token1"><</span><span class="token1">?</span>php
<span class="token4">define</span><span class="token3">(</span><span class="token2">'ROOTPATH'</span><span class="token3">,</span>__DIR__<span class="token3">.</span>DIRECTORY_SEPARATOR<span class="token3">)</span><span class="token3">;</span>
<span class="token">/**
* 获取激活的插件
* @return array 返回激活的插件信息数组
*/</span>
<span class="token5">function</span> <span class="token4">get_active_plugins</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token">//一般从数据库拉出 这里用数据模拟</span>
$plugins<span class="token1">=</span><span class="token3">[</span>
<span class="token3">[</span>
<span class="token2">'name'</span> <span class="token1">=</span><span class="token1">></span> <span class="token2">'demo'</span><span class="token3">,</span>
<span class="token2">'directory'</span><span class="token1">=</span><span class="token1">></span><span class="token2">'demo'</span>
<span class="token3">]</span><span class="token3">,</span>
<span class="token3">]</span><span class="token3">;</span>
<span class="token5">return</span> $plugins<span class="token3">;</span>
<span class="token3">}</span>
require_once <span class="token2">"./core/PluginManager.php"</span><span class="token3">;</span>
$pluginManager<span class="token1">=</span><span class="token5">new</span> <span class="token4">PluginManager</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//trigger通知观察者(插件)</span>
$pluginManager<span class="token1">-</span><span class="token1">></span><span class="token4">trigger</span><span class="token3">(</span><span class="token2">'demo'</span><span class="token3">,</span><span class="token2">''</span><span class="token3">)</span><span class="token3">;</span>
```
```
> 插件管理类 /core/PluginManager.php
```
<pre class="calibre10">```
<span class="token1"><</span><span class="token1">?</span>php
class <span class="token4">PluginManager</span><span class="token3">{</span>
private $_listeners<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span><span class="token4">_init</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
private <span class="token5">function</span> <span class="token4">_init</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token">//外部获取所有活动插件,返回包含插件名及插件目录的数组</span>
$plugins<span class="token1">=</span><span class="token4">get_active_plugins</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//循环实例激活</span>
foreach <span class="token3">(</span>$plugins as $plugin<span class="token3">)</span> <span class="token3">{</span>
$plugin_file<span class="token1">=</span>ROOTPATH<span class="token3">.</span><span class="token2">'plugins'</span><span class="token3">.</span>DIRECTORY_SEPARATOR<span class="token3">.</span>$plugin<span class="token3">[</span><span class="token2">'directory'</span><span class="token3">]</span><span class="token3">.</span>DIRECTORY_SEPARATOR<span class="token3">.</span><span class="token2">'actions.php'</span><span class="token3">;</span>
<span class="token5">if</span> <span class="token3">(</span>@<span class="token4">file_exists</span><span class="token3">(</span>$plugin_file<span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span>
<span class="token">//加载这些插件的文件</span>
<span class="token4">include_once</span><span class="token3">(</span>$plugin_file<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span><span class="token5">else</span><span class="token3">{</span>
<span class="token5">throw</span> <span class="token5">new</span> <span class="token4">Exception</span><span class="token3">(</span><span class="token2">'文件'</span><span class="token3">.</span>$plugin_file<span class="token3">.</span><span class="token2">'不存在!'</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//解析出这些插件的插件类名</span>
$class <span class="token1">=</span> <span class="token4">strtolower</span><span class="token3">(</span>$plugin<span class="token3">[</span><span class="token2">'name'</span><span class="token3">]</span><span class="token3">)</span><span class="token3">.</span><span class="token2">'_actions'</span><span class="token3">;</span><span class="token">//如demo_actions</span>
<span class="token5">if</span><span class="token3">(</span><span class="token4">class_exists</span><span class="token3">(</span>$class<span class="token3">)</span><span class="token3">)</span><span class="token3">{</span>
<span class="token">//实例化插件类 同时实例化的插件析构方法调用register注册插件(将实例放入$_listeners中)</span>
<span class="token">//等同 new demo_actions(PluginManager $this); $pluginManager->register('demo', $demo_actions_instance, 'say_hello');</span>
<span class="token5">new</span> $<span class="token4">class</span><span class="token3">(</span>$this<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span><span class="token5">else</span><span class="token3">{</span>
<span class="token5">throw</span> <span class="token5">new</span> <span class="token4">Exception</span><span class="token3">(</span>$class<span class="token3">.</span><span class="token2">'不存在!'</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">/**
* 触发一个钩子
*
* @param string $hook 钩子的名称
* @param mixed $data 钩子的入参
* @return mixed
*/</span>
public <span class="token5">function</span> <span class="token4">trigger</span><span class="token3">(</span>$hook<span class="token3">,</span>$data<span class="token3">)</span><span class="token3">{</span>
$result <span class="token1">=</span> <span class="token2">''</span><span class="token3">;</span>
<span class="token5">if</span> <span class="token3">(</span><span class="token4">isset</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_listeners<span class="token3">[</span>$hook<span class="token3">]</span><span class="token3">)</span> <span class="token1">&&</span> <span class="token4">is_array</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_listeners<span class="token3">[</span>$hook<span class="token3">]</span><span class="token3">)</span> <span class="token1">&&</span> <span class="token4">count</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_listeners<span class="token3">[</span>$hook<span class="token3">]</span><span class="token3">)</span> <span class="token1">></span> <span class="token6">0</span><span class="token3">)</span><span class="token3">{</span>
<span class="token4">var_dump</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_listeners<span class="token3">)</span><span class="token3">;</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_listeners<span class="token3">[</span>$hook<span class="token3">]</span> as $listener<span class="token3">)</span> <span class="token3">{</span>
<span class="token">//他的key就是 demo_actions->say_hello</span>
<span class="token">// 取出插件对象的引用和方法</span>
$class <span class="token1">=</span><span class="token1">&</span> $listener<span class="token3">[</span><span class="token6">0</span><span class="token3">]</span><span class="token3">;</span>
$method <span class="token1">=</span> $listener<span class="token3">[</span><span class="token6">1</span><span class="token3">]</span><span class="token3">;</span>
<span class="token5">if</span><span class="token3">(</span><span class="token4">method_exists</span><span class="token3">(</span>$class<span class="token3">,</span>$method<span class="token3">)</span><span class="token3">)</span>
<span class="token3">{</span>
<span class="token">// 动态调用插件的方法</span>
$result <span class="token3">.</span><span class="token1">=</span> $class<span class="token1">-</span><span class="token1">></span>$<span class="token4">method</span><span class="token3">(</span>$data<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token5">return</span> $result<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">/**
* 注册插件
* @param string $hook 钩子的名称
* @param obj &$reference 插件类实例的引用
* @param string $method 插件所执行的方法(插件入口方法)
* @return void
*/</span>
public <span class="token5">function</span> <span class="token4">register</span><span class="token3">(</span>$hook<span class="token3">,</span> <span class="token1">&</span>$reference<span class="token3">,</span> $method<span class="token3">)</span><span class="token3">{</span>
<span class="token">//获取插件要实现的方法</span>
$key <span class="token1">=</span> <span class="token4">get_class</span><span class="token3">(</span>$reference<span class="token3">)</span><span class="token3">.</span><span class="token2">'->'</span><span class="token3">.</span>$method<span class="token3">;</span>
<span class="token">//将插件的引用连同方法push进监听数组即$_listeners属性中</span>
$this<span class="token1">-</span><span class="token1">></span>_listeners<span class="token3">[</span>$hook<span class="token3">]</span><span class="token3">[</span>$key<span class="token3">]</span> <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span><span class="token1">&</span>$reference<span class="token3">,</span> $method<span class="token3">)</span><span class="token3">;</span>
#此处做些日志记录方面的东西
<span class="token3">}</span>
<span class="token3">}</span>
```
```
> /plugins/demo/actions.php
```
<pre class="calibre10">```
<span class="token1"><</span><span class="token1">?</span>php
<span class="token">/**
*需要注意的几个默认规则:
* 1. 本插件类的文件名必须是action
* 2. 插件类的名称必须是{插件名_actions}
*/</span>
class <span class="token4">demo_actions</span>
<span class="token3">{</span>
<span class="token">//解析函数的参数是pluginManager的引用</span>
<span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>PluginManager <span class="token1">&</span>$pluginManager<span class="token3">)</span>
<span class="token3">{</span>
<span class="token">//注册这个插件</span>
<span class="token">//第一个参数是钩子的名称</span>
<span class="token">//第二个参数是该类实例的引用</span>
<span class="token">//第三个是插件所执行的方法</span>
$pluginManager<span class="token1">-</span><span class="token1">></span><span class="token4">register</span><span class="token3">(</span><span class="token2">'demo'</span><span class="token3">,</span> $this<span class="token3">,</span> <span class="token2">'say_hello'</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token5">function</span> <span class="token4">say_hello</span><span class="token3">(</span><span class="token3">)</span>
<span class="token3">{</span>
echo <span class="token2">'Hello World'</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
```
```
## [php插件机制原理之其他实现方式例子](https://www.jianshu.com/p/7747bbbd14c0)
- php更新内容
- 其他
- empty、isset、is_null
- echo 输出bool值
- if真假情况
- 常量
- define与const(php5.3) 类常量
- 递归
- 单元测试
- 面向对象
- 全局变量域超全局变量
- php网络相关
- 支持的协议和封装协议(如http,php://input)
- 上下文(Context)选项和参数
- 过滤器
- http请求及模拟登录
- socket
- streams
- swoole
- 超全局变量
- $_ENV :存储了一些系统的环境变量
- $_COOKIE
- $_SESSION
- $_FILES
- $_SERVER
- 正则
- php正则函数
- 去除文本中的html、xml的标签
- 特殊符号
- \r\n
- 模式修正符
- 分组
- 断言
- 条件表达式
- 递归表达式 (?R)
- 固化分组
- 正则例子
- 框架
- 自动加载spl_autoload_register
- 时间函数
- 文件操作
- 文件的上传下载
- 常见的mimi类型
- 文件断点续传
- 下载文件防盗链
- 破解防盗链
- 无限分类
- 短信验证码
- 短信宝
- 视频分段加载
- phpDoc注释
- 流程控制代替语法
- 三元运算
- @错误抑制符
- 字符编码
- PHP CLI模式开发
- 配置可修改范围
- CGI、FastCGI和PHP-FPM关系图解
- No input file specified的解决方法
- SAPI(PHP常见的四种运行模式)
- assert断言
- 类基础
- 类的三大特性:封装,继承,多态
- 魔术方法
- 辅助查询(*)
- extends继承
- abstract 抽象类
- interface 接口(需要implements实现)
- 抽象类和接口的区别
- 多态
- static
- final
- serialize与unserialize
- instanceof 判断后代子类
- 类型约束
- clone克隆
- ::的用法
- new self()与new static()
- this、self、static、parent、super
- self、static、parent:后期静态绑定
- PHP的静态变量
- php导入
- trait
- 动态调用类方法
- 参数及类型申明
- 方法的重载覆盖
- return $a && $b
- 设计思想
- 依赖注入与依赖倒置
- 创建型模式(创建类对象)
- (*)单例模式
- (*)工厂模式
- 原型模式(在方法里克隆this)
- 创建者模式
- 结构型模式
- 适配器模式(Adapter)
- 桥接模式
- 装饰模式
- 组合模式
- 外观模式(门面(Facade)模式)
- 享元模式
- 代理模式
- 行为型模式
- (*)观察者模式
- (*)迭代器模式(Iterator)
- 模板方法模式 Template
- 命令模式(Command)
- 中介者模式(Mediator)
- 状态模式(State)
- 职责链模式 (Chainof Responsibility)
- 策略模式(Strategy)
- 已知模式-备忘录模式(Memento)
- 深度模式-解释器模式(Interpreter)
- 深度模式-访问者模式(Visitor)
- (*)注册树(注射器、注册表)模式
- 函数参考
- 影响 PHP 行为的扩展
- APC扩展(过时)
- APCu扩展
- APD扩展(过时)
- bcompiler扩展(过时)
- BLENC扩展 (代码加密 实验型)
- Componere扩展(7.1+)
- 错误处理扩展(PHP 核心)
- FFI扩展
- htscanner扩展
- inclued扩展
- Memtrack扩展
- OPcache扩展(5.5.0内部集成)
- Output Control扩展(核心)
- PHP Options/Info扩展(核心)
- phpdbg扩展(5.6+内部集成)
- runkit扩展
- runkit7扩展
- scream扩展
- uopz扩展
- Weakref扩展
- WinCache扩展
- Xhprof扩展
- 音频格式操作
- ID3
- KTaglib
- oggvorbis
- OpenAL
- 身份认证服务
- KADM5
- Radius
- 针对命令行的扩展
- Ncurses(暂无人维护)
- Newt(暂无人维护)
- Readline
- 压缩与归档扩展
- Bzip2
- LZF
- Phar
- Rar
- Zip
- Zlib
- 信用卡处理
- 加密扩展
- Crack(停止维护)
- CSPRNG(核心)
- Hash扩展(4.2内置默认开启、7.4核心)
- Mcrypt(7.2移除)
- Mhash(过时)
- OpenSSL(*)
- 密码散列算法(核心)
- Sodium(+)
- 数据库扩展
- 数据库抽象层
- 针对各数据库系统对应的扩展
- 日期与时间相关扩展
- Calendar
- 日期/时间(核心)
- HRTime(*)
- 文件系统相关扩展
- Direct IO
- 目录(核心)
- Fileinfo(内置)
- 文件系统(核心)
- Inotify
- Mimetype(过时)
- Phdfs
- Proctitle
- xattr
- xdiff
- 国际化与字符编码支持
- Enchant
- FriBiDi
- Gender
- Gettext
- iconv(内置默认开启)
- intl
- 多字节字符串(mbstring)
- Pspell
- Recode(将要过时)
- 图像生成和处理
- Cairo
- Exif
- GD(内置)
- Gmagick
- ImageMagick
- 邮件相关扩展
- Cyrus
- IMAP
- Mail(核心)
- Mailparse
- vpopmail(实验性 )
- 数学扩展
- BC Math
- GMP
- Lapack
- Math(核心)
- Statistics
- Trader
- 非文本内容的 MIME 输出
- FDF
- GnuPG
- haru(实验性)
- Ming(实验性)
- wkhtmltox(*)
- PS
- RPM Reader(停止维护)
- RpmInfo
- XLSWriter Excel操作(*)
- 进程控制扩展
- Eio
- Ev
- Expect
- Libevent
- PCNTL
- POSIX
- 程序执行扩展(核心)
- parallel
- pthreads(*)
- pht
- Semaphore
- Shared Memory
- Sync
- 其它基本扩展
- FANN
- GeoIP(*)
- JSON(内置)
- Judy
- Lua
- LuaSandbox
- Misc(核心)
- Parsekit
- SeasLog(-)
- SPL(核心)
- SPL Types(实验性)
- Streams(核心)
- Swoole(*)
- Tidy扩展
- Tokenizer
- URLs(核心)
- V8js(*)
- Yaml
- Yaf
- Yaconf(核心)
- Taint(检测xss字符串等)
- Data Structures
- 其它服务
- 网络(核心)
- cURL(*)
- Event(*)
- chdb
- FAM
- FTP
- Gearman
- Gopher
- Gupnp
- Hyperwave API(过时)
- LDAP(+)
- Memcache
- Memcached(+)
- mqseries
- RRD
- SAM
- ScoutAPM
- SNMP
- Sockets
- SSH2
- Stomp
- SVM
- SVN(试验性的)
- TCP扩展
- Varnish
- YAZ
- YP/NIS
- 0MQ(ZeroMQ、ZMQ)消息系统
- ZooKeeper
- 搜索引擎扩展
- mnoGoSearch
- Solr
- Sphinx
- Swish(实验性)
- 针对服务器的扩展
- Apache
- FastCGI 进程管理器
- IIS
- NSAPI
- Session 扩展
- Msession
- Sessions
- Session PgSQL
- 文本处理
- BBCode
- CommonMark(markdown解析)
- Parle
- PCRE( 核心)
- POSIX Regex
- ssdeep
- 字符串(核心)
- 变量与类型相关扩展
- 数组(核心)
- 类/对象(核心)
- Classkit(未维护)
- Ctype
- Filter扩展
- 函数处理(核心)
- quickhash扩展
- 反射扩展(核心)
- Variable handling(核心)
- Web 服务
- OAuth
- SCA(实验性)
- SOAP
- Yar
- XML-RPC(实验性)
- Windows 专用扩展
- COM
- win32ps
- win32service
- XML 操作
- DOM(内置,默认开启)
- libxml(内置 默认开启)
- SDO(停止维护)
- SDO-DAS-Relational(试验性的)
- SDO DAS XML
- SimpleXML(内置,5.12+默认开启)
- WDDX
- XMLDiff
- XML 解析器(Expat 解析器 默认开启)
- XMLReader(5.1+内置默认开启)
- XMLWriter(5.1+内置默认开启)
- XSL(内置)
- 图形用户界面(GUI) 扩展
- UI
- 预定义类
- PHP SPL(PHP 标准库)
- 数据结构
- SplDoublyLinkedList(双向链表)
- SplStack(栈 先进后出)
- SplQueue(队列)
- SplHeap(堆)
- SplMaxHeap(最大堆)
- SplMinHeap(最小堆)
- SplPriorityQueue(堆之优先队列)
- SplFixedArray(阵列【数组】)
- SplObjectStorage(映射【对象存储】)
- 迭代器
- DirectoryIterator类
- 文件处理
- SplFileInfo
- SplFileObject
- SplTempFileObject
- 接口 interface
- Countable
- OuterIterator
- RecursiveIterator
- SeekableIterator
- 异常
- 各种类及接口
- SplSubject
- SplObserver
- ArrayObject(将数组作为对象操作)
- SPL 函数
- 预定义接口
- Traversable(遍历)接口
- Iterator(迭代器)接口
- IteratorAggregate(聚合式迭代器)接口
- ArrayAccess(数组式访问)接口
- Serializable 序列化接口
- JsonSerializable
- Closure 匿名函数(闭包)类
- Generator生成器类
- 生成器(php5.5+)
- 反射
- 一、反射(reflection)类
- 二、Reflector 接口
- ReflectionClass 类报告了一个类的有关信息。
- ReflectionFunctionAbstract
- ReflectionParameter 获取函数或方法参数的相关信息
- ReflectionProperty 类报告了类的属性的相关信息。
- ReflectionClassConstant类报告有关类常量的信息。
- ReflectionZendExtension 类返回Zend扩展相关信息
- ReflectionExtension 报告了一个扩展(extension)的有关信息。
- 三、ReflectionGenerator类用于获取生成器的信息
- 四、ReflectionType 类用于获取函数、类方法的参数或者返回值的类型。
- 五、反射的应用场景
- git
- Git代码同时上传到GitHub和Gitee(码云)
- Git - 多人协同开发利器,团队协作流程规范与注意事项
- 删除远程仓库的文件
- 创建composer项目
- composer安装及设置
- composer自动加载讲解
- phpsdudy的composer操作
- swoole笔记
- 安装及常用Cli操作
- TCP
- 4种回调函数的写法
- phpRedis
- API
- API详细
- redis DB 概念:
- 通用命令:rawCommand
- Connection
- Server
- List
- Set
- Zset
- Hash
- string
- Keys
- 事物
- 发布订阅
- 流streams
- Geocoding 地理位置
- lua脚本
- Introspection 自我检测
- biMap
- 原生
- php-redis 操作类 封装
- redis 队列解决秒杀解决超卖:
- Linux
- Centos8(Liunx) 中安装PHP7.4 的三种方法和删除它的三种方法
- 权限设计
- ACL
- RBAC
- RBAC0
- RBAC1
- RBAC2
- RBAC3
- 例子
- ABAC 基于属性的访问控制
- 总结:SAAS后台权限设计案例分析
- casbin-权限管理框架
- 开始使用
- casbinAPI
- Think-Casbin
- 单点登录(SSO)
- OAuth授权
- OAuth 2.0 的四种方式
- 更新令牌
- 例子:第三方登录
- 微服务架构下的统一身份认证和授权
- 杂项
- SSL证书
- sublime Emmet的快捷语法
- 免费翻译接口
- 免费空间
- xss过滤
- HTML Purifier文档
- xss例子
- 实用小函数
- PHP操作Excel
- 架构师必须知道的26项PHP安全实践
- 模版布局
- smarty模版
- blade
- twig
- 大佬博客
- 优化
- 缓存
- opcache
- memcache
- 数据库
- 主从分布
- 数据库设计
- 笔记
- 配置
- nginx 主从配置
- nginx 负载均衡的配置
- 手动搭建Redis集群和MySQL主从同步(非Docker)
- Redis Cluster集群
- mysql主从同步
- 用安卓手机搭建 web 服务器
- 软件选择
- 扩展库列表
- 代码审计
- 漏洞挖掘的思路
- 命令注入
- 代码注入
- XSS 反射型漏洞
- XSS 存储型漏洞
- 本地包含与远程包含
- sql注入
- 函数
- 注释
- 步骤
- information_schema
- sql注入的分类
- 实战
- 防御
- CSRF 跨站请求伪造
- 计动态函数执行与匿名函数执行
- unserialize反序列化漏洞
- 覆盖变量漏洞
- 文件管理漏洞
- 文件上传漏洞
- 跳过登录
- URL编码对照表
- 浏览器插件开发
- 插件推荐
- 扩展文件manifest.json
- 不可视的background(常驻)页面
- 可视页面browser actions与page actions及八种展示方式
- 使用chrome.xxx API
- Google Chrome扩展与Web页面/服务器之间的交互
- Google Chrome扩展中的页面之间的数据通信
- inject-script
- chromeAPI
- pageAction
- alarms
- chrome.tabs
- chrome.runtime
- chrome.webRequest
- chrome.window
- chrome.storage
- chrome.contextMenus
- chrome.devtools
- chrome.extension
- 分类
- homepage_url 开发者或者插件主页
- 5种类型的JS对比及消息通信
- 其它补充
- 前端、移动端
- html5
- meta标签
- flex布局
- javascript
- 获取js对象所有方法
- dom加载
- ES6函数写法
- ES6中如何导入和导出模块
- 数组的 交集 差集 补集 并集
- bootstrap
- class速查
- 常见data属性
- 开源项目
- 会员 数据库表设计
- 程序执行
- 开发总结
- API接口
- API接口设计
- json转化
- app接口