PHP Advanced Programming (3) _php tutorial

Source: Internet
Author: User
Tags spl
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.