> php教程 > php手册 > PHP 高级编程(3)

PHP 高级编程(3)

WBOY
풀어 주다: 2016-06-13 09:33:23
원래의
1187명이 탐색했습니다.

SPL(标准PHP库 - Standard PHP Library)是PHP5面向对象功能中重要的部分。原文解释是这样的“The Standard PHP Library (SPL) is a collection of interfaces and classes that are meant to solve common problems”。

SplSubject 和 SplObserver 接口

The SplSubject interface is used alongside SplObserver to implement the Observer Design Pattern.

观察者模式是一种简单的事件系统,包含了两个或更多的互相交互的类。这一模式允许某个类观察另一个类的状态,当被观察类的状态发生变化时,这个模式会得到通知。被观察的类叫subject,负责观察的类叫做Observer 。PHP 提供的 SplSubject 和 SplObserver接口可用来表达这些内容。

<span>SplSubject {
</span><span>/*</span><span> 方法 </span><span>*/</span>
<span>abstract</span> <span>public</span> <span>void</span><span> attach ( SplObserver $observer )
</span><span>abstract</span> <span>public</span> <span>void</span><span> detach ( SplObserver $observer )
</span><span>abstract</span> <span>public</span> <span>void</span> notify ( <span>void</span><span> )
}</span>
로그인 후 복사
<span>SplObserver {
</span><span>/*</span><span> 方法 </span><span>*/</span>
<span>abstract</span> <span>public</span> <span>void</span><span> update ( SplSubject $subject )
}</span>
로그인 후 복사

这里,splsubject类维护了一个特定状态,当这个状态发生变化时,他就会调用notify方法,所以之前使用attach注册的splobserver实例的update就会被调用。这里我们实现一个简单地观察者模式的例子

<?<span>php
</span><span>/*</span><span>*
 * Subject,that who makes news
 </span><span>*/</span>
<span>class</span><span> Newspaper implements \SplSubject{
    </span><span>private</span><span> $name;
    </span><span>private</span> $observers =<span> array();
    </span><span>private</span><span> $content;
    
    </span><span>public</span><span> function __construct($name) {
        $</span><span>this</span>->name =<span> $name;
    }

    </span><span>//</span><span>add observer</span>
    <span>public</span><span> function attach(\SplObserver $observer) {
        $</span><span>this</span>->observers[] =<span> $observer;
    }
    
    </span><span>//</span><span>remove observer</span>
    <span>public</span><span> function detach(\SplObserver $observer) {
        
        $key </span>= array_search($observer,$<span>this</span>->observers, <span>true</span><span>);
        </span><span>if</span><span>($key){
            unset($</span><span>this</span>-><span>observers[$key]);
        }
    }
    
    </span><span>//</span><span>set breakouts news</span>
    <span>public</span><span> function breakOutNews($content) {
        $</span><span>this</span>->content =<span> $content;
        $</span><span>this</span>-><span>notify();
    }
    
    </span><span>public</span><span> function getContent() {
        </span><span>return</span> $<span>this</span>->content.<span>"</span><span> ({$this->name})</span><span>"</span><span>;
    }
    
    </span><span>//</span><span>notify observers(or some of them)</span>
    <span>public</span><span> function notify() {
        </span><span>foreach</span> ($<span>this</span>->observers <span>as</span><span> $value) {
            $value</span>->update($<span>this</span><span>);
        }
    }
}

</span><span>/*</span><span>*
 * Observer,that who recieves news
 </span><span>*/</span>
<span>class</span><span> Reader implements SplObserver{
    </span><span>private</span><span> $name;
    
    </span><span>public</span><span> function __construct($name) {
        $</span><span>this</span>->name =<span> $name;
    }
    
    </span><span>public</span><span> function update(\SplSubject $subject) {
        echo $</span><span>this</span>->name.<span>'</span><span> is reading breakout news <b></span><span>'</span>.$subject->getContent().<span>'</span><span></b><br></span><span>'</span><span>;
    }
}

$newspaper </span>= <span>new</span> Newspaper(<span>'</span><span>Newyork Times</span><span>'</span><span>);

$allen </span>= <span>new</span> Reader(<span>'</span><span>Allen</span><span>'</span><span>);
$jim </span>= <span>new</span> Reader(<span>'</span><span>Jim</span><span>'</span><span>);
$linda </span>= <span>new</span> Reader(<span>'</span><span>Linda</span><span>'</span><span>);

</span><span>//</span><span>add reader</span>
$newspaper-><span>attach($allen);
$newspaper</span>-><span>attach($jim);
$newspaper</span>-><span>attach($linda);

</span><span>//</span><span>remove reader</span>
$newspaper-><span>detach($linda);

</span><span>//</span><span>set break outs</span>
$newspaper->breakOutNews(<span>'</span><span>USA break down!</span><span>'</span><span>);

</span><span>//</span><span>=====output======
</span><span>//</span><span>Allen is reading breakout news USA break down! (Newyork Times)
</span><span>//</span><span>Jim is reading breakout news USA break down! (Newyork Times)</span>
로그인 후 복사

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿