PHP observer mode PHPSplSubjectSplObserver interface implementation

Source: Internet
Author: User
In fact, the Observer Mode is a mode that is easier to understand. it is an event system, which means that this mode allows a class to observe the status of another class, when the status of the observed class changes, the observed class can receive notifications and make corresponding actions. For example, when I press the keyboard, the system will receive a notification and respond accordingly.

In fact, the Observer Mode is a mode that is easier to understand. it is an event system, which means that this mode allows a class to observe the status of another class, when the status of the observed class changes, the observed class can receive notifications and make corresponding actions. For example, when I press the keyboard, the system will receive a notification and respond accordingly.

For PHP, PHP provides two built-in interfaces for external application areas to implement this mode.

SplSubject interface, which represents the object to be observed,

Its structure:

 

 

 

SplObserver interface, which represents the object that acts as the Observer,
Its structure:

 

  

 

This mode is implemented in this way. SplSubject maintains a specific state. when this state changes, it uses notify Y () to notify all splobservers registered to SplSubject with attach, and call the corresponding update method.
Simple example:

 

   observers = array();                 }              public function attach(SplObserver $observer){$this->observers[] = $observer;}              public function detach(SplObserver $observer){                     if($idx = array_search($observer, $this->observers, true)) {                            unset($this->observers[$idx]);                          }              }              public function notify(){                     foreach($this->observers as $observer){                            $observer->update($this);                     }              }              public function setValue($value){                     $this->value = $value;                     $this->notify();              }              public function getValue(){                     return $this->value;                   }       }        class observer implements SplObserver{              public function update(SplSubject $subject){                     echo 'The new state of subject'.$subject->getValue();               }           }        $subject = new subject();       $observer = new observer();       $subject->attach($observer);       $subject->setValue(5);

 

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.