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