Home >Backend Development >PHP Tutorial >PHP Advanced Programming (3)_PHP Tutorial

PHP Advanced Programming (3)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:27:411068browse

SPL (Standard PHP Library - Standard PHP Library) is an important part of the object-oriented functionality of PHP5. The original explanation is this: "The Standard PHP Library (SPL) is a collection of interfaces and classes that are meant to solve common problems."

SplSubject and SplObserver interfaces

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

The Observer pattern is a simple event system that contains two or more classes that interact with each other. This pattern allows one class to observe the state of another class and be notified when the state of the observed class changes. The class being observed is called subject, and the class responsible for observing is called Observer. The SplSubject and SplObserver interfaces provided by PHP can be used to express these contents.

<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>

Here, the splsubject class maintains a specific state. When this state changes, it will call the notify method, so the update of the splobserver instance previously registered using attach will be called. Here we implement a simple example of the observer pattern

<?<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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/814842.htmlTechArticleSPL (Standard PHP Library - Standard PHP Library) is an important part of the object-oriented functionality of PHP5. The original explanation is this: The Standard PHP Library (SPL) is a collection of interfaces and c...
Statement:
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