PHP advanced programming (3) _ PHP Tutorial

Source: Internet
Author: User
Tags spl
PHP advanced programming (3 ). SPL (Standard PHP library-StandardPHPLibrary) is an important part of PHP5 object-oriented functions. In the original article, TheStandardPHPLibrary (SPL) isacollectionofinterfacesandc SPL (Standard PHP Library) is an important part of PHP5 object-oriented functions. The original article explains "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 mode is a simple event system that contains two or more classes that interact with each other. This mode allows a class to observe the status of another class. when the status of the observed class changes, this mode will be notified. The observed class is called subject, and the class to be observed is called Observer. SplSubject and SplObserver interfaces provided by PHP can be used to express these contents.

SplSubject {/* method */abstract public void attach (SplObserver $ observer) abstract public void detach (SplObserver $ observer) abstract public void policy (void )}
SplObserver {/* method */abstract public void update (SplSubject $ subject )}

The splsubject class maintains a specific state. when this state changes, it will call the notify method. Therefore, the update of the splobserver instance registered with attach will be called. Here is an example of implementing a simple observer mode.

 name = $name;    }    //add observer    public function attach(\SplObserver $observer) {        $this->observers[] = $observer;    }        //remove observer    public function detach(\SplObserver $observer) {                $key = array_search($observer,$this->observers, true);        if($key){            unset($this->observers[$key]);        }    }        //set breakouts news    public function breakOutNews($content) {        $this->content = $content;        $this->notify();    }        public function getContent() {        return $this->content." ({$this->name})";    }        //notify observers(or some of them)    public function notify() {        foreach ($this->observers as $value) {            $value->update($this);        }    }}/** * Observer,that who recieves news */class Reader implements SplObserver{    private $name;        public function __construct($name) {        $this->name = $name;    }        public function update(\SplSubject $subject) {        echo $this->name.' is reading breakout news '.$subject->getContent().'
'; }}$newspaper = new Newspaper('Newyork Times');$allen = new Reader('Allen');$jim = new Reader('Jim');$linda = new Reader('Linda');//add reader$newspaper->attach($allen);$newspaper->attach($jim);$newspaper->attach($linda);//remove reader$newspaper->detach($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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/814842.htmlTechArticleSPL (Standard PHP Library) is an important part of PHP5 object-oriented functions. The original article explains that 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.