PHP 高级编程(3)
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
