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

PHP Advanced Programming (3)_PHP Tutorial

Jul 13, 2016 am 10:27 AM
php spl Design Patterns

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>
Copy after login
<span>SplObserver {
</span><span>/*</span><span> 方法 </span><span>*/</span>
<span>abstract</span> <span>public</span> <span>void</span><span> update ( SplSubject $subject )
}</span>
Copy after login

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

&lt;?<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>-&gt;name =<span> $name;
    }

    </span><span>//</span><span>add observer</span>
    <span>public</span><span> function attach(\SplObserver $observer) {
        $</span><span>this</span>-&gt;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>-&gt;observers, <span>true</span><span>);
        </span><span>if</span><span>($key){
            unset($</span><span>this</span>-&gt;<span>observers[$key]);
        }
    }
    
    </span><span>//</span><span>set breakouts news</span>
    <span>public</span><span> function breakOutNews($content) {
        $</span><span>this</span>-&gt;content =<span> $content;
        $</span><span>this</span>-&gt;<span>notify();
    }
    
    </span><span>public</span><span> function getContent() {
        </span><span>return</span> $<span>this</span>-&gt;content.<span>"</span><span> ({$this-&gt;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>-&gt;observers <span>as</span><span> $value) {
            $value</span>-&gt;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>-&gt;name =<span> $name;
    }
    
    </span><span>public</span><span> function update(\SplSubject $subject) {
        echo $</span><span>this</span>-&gt;name.<span>'</span><span> is reading breakout news &lt;b&gt;</span><span>'</span>.$subject-&gt;getContent().<span>'</span><span>&lt;/b&gt;&lt;br&gt;</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-&gt;<span>attach($allen);
$newspaper</span>-&gt;<span>attach($jim);
$newspaper</span>-&gt;<span>attach($linda);

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

</span><span>//</span><span>set break outs</span>
$newspaper-&gt;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>
Copy after login

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles