A simple example of observer mode in PHP, PHP watcher Instance _php tutorial

Source: Internet
Author: User

Simple example of observer pattern in PHP, PHP watcher Instance


The Observer pattern is one of the more common patterns in design patterns, with two or more classes interacting with one another. This pattern allows a class to observe the state of another class, and when the state of the observed class changes, the observer will be notified to update the corresponding state.

PHP's SPL Standard class library provides splsubject and Splobserver interfaces for implementation, the observed class is called subject, and the class that is responsible for the observation is called Observer. This pattern is a splsubject class that maintains a specific state,

When this state changes, it calls the Notify method. When the Notify method is called, all the update methods of the Splobserver instance that were previously registered using the Attach method are called, with the following demo:
Copy CodeThe code is as follows:
Class Demosubject implements splsubject{
Private $observers, $value;

Public Function __construct () {
$this->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 Demoobserver implements splobserver{
Public Function Update (Splsubject $subject) {
Echo ' The new value is '. $subject->getvalue ();
}
}

$subject = new Demosubject ();
$observer = new Demoobserver ();
$subject->attach ($observer);
$subject->setvalue (5);

http://www.bkjia.com/PHPjc/945719.html www.bkjia.com true http://www.bkjia.com/PHPjc/945719.html techarticle A simple example of observer patterns in PHP, the PHP Watcher Instance Observer pattern is a more common pattern in design patterns, containing two or more classes interacting with each other. This mode allows ...

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