SPL, the standard PHP library, is an important part of the PHP5 object-oriented functionality. "The standard PHP Library (SPL) was a collection of interfaces and classes that was 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 a class to observe the state of another class, which is notified when the state of the observed class changes. The observed class is called subject, and the class of observation is called Observer. The Splsubject and Splobserver interfaces provided by PHP can be used to express these things.
splsubject {/**/abstractpublicvoid Attach ( Splobserver $observer)abstractpublicvoid detach (Splobserver $observer) Abstractpublicvoidvoid )}
splobserver {/** */abstractpublicvoid Update ( Splsubject $subject)}
Here, the Splsubject class maintains a specific state, and when that state changes, he calls the Notify method, so that the update of the Splobserver instance that was previously registered with attach is called. Here we implement an example of a simple observer pattern
Php/** * Subject,that who makes news*/classNewspaper implements \splsubject{Private$name; Private$observers =Array (); Private$content; Publicfunction __construct ($name) {$ This->name =$name; } //Add Observer Publicfunction Attach (\splobserver $observer) {$ This->observers[] =$observer; } //Remove Observer Publicfunction Detach (\splobserver $observer) {$key= Array_search ($observer, $ This->observers,true); if($key) {unset ($ This-observers[$key]); } } //Set Breakouts News Publicfunction Breakoutnews ($content) {$ This->content =$content; $ This-notify (); } Publicfunction GetContent () {return$ This->content."({$this->name})"; } //notify observers (or some of them) Publicfunction Notify () {foreach($ This->observers as$value) {$value->update ($ This); } }}/** * Observer,that who recieves news*/classReader implements splobserver{Private$name; Publicfunction __construct ($name) {$ This->name =$name; } Publicfunction Update (\splsubject $subject) {echo $ This->name.'is reading breakout news '. $subject->getcontent (). '
'; }} $newspaper=NewNewspaper ('NewYork Times'); $allen=NewReader ('Allen'); $jim=NewReader ('Jim'); $linda=NewReader ('Linda');//Add Reader$newspaperAttach ($allen); $newspaper-Attach ($jim); $newspaper-Attach ($linda);//Remove Reader$newspaperdetach ($linda);//Set Break Outs$newspaper->breakoutnews ('USA Break down!');//=====output======//Allen is reading breakout news USA break down! (NewYork times)//Jim is reading breakout news USA break down! (NewYork times)
http://www.bkjia.com/PHPjc/814842.html www.bkjia.com true http://www.bkjia.com/PHPjc/814842.html techarticle SPL, the standard PHP library, is an important part of the PHP5 object-oriented functionality. The original explanation is this. The Standard PHP Library (SPL) is a collection of interfaces and C ...