Example Analysis of PHP Observer Pattern Definition and Usage

  • 2021-12-04 09:34:00
  • OfStack

This paper illustrates the definition and usage of PHP observer pattern. Share it for your reference, as follows:

The observer pattern I understand is that when we fire an event, we want to tell the object I want to tell, and let them all perform this operation, that is, bind them to this event.

1. Define a base class (for adding observers, which is the object I want to notify, and notifying observers)

EventGenerator.php


<?php
abstract class EventGenerator
{
  // Stores the observer, because there can be multiple objects to be notified, so it is 1 Array of numbers 
  private $observers = array();
  // Add an observer, which is the object I want to notify 
  public function addObserver($observer)
  {
    $this->observers[] = $observer;
  }
  // Inform the observer 
  public function notify()
  {
    // Fetch all the objects to perform the operation 
    foreach($this->observers as $observer)
    {
      // Here I write 1 A update Method, which requires that all my notification objects must have update Operation, so 1 Yes, I will define 1 An excuse for all observers to force this method 
      $observer->update();
    }
  }
}
?>

Define the interface file for all observers to implement the update method Observer. php


<?php
interface Observer
{
  function update();
}
?>

Defines the file Event. php that implements the Observer pattern


<?php
header('Content-Type:text/html;charset=utf-8');
define('BASEDIR', __DIR___);
require 'Config.php';
spl_autoload_register('Config::autoload');
// Defining an event class inherits a base class 
class Event extends EventGenerator
{
  // Notify all observers when this event is triggered 
  public function trigger()
  {
    echo ' I'm going to start updating! <br/>\n';
    // Notification method of base class 
    $this->notify();
  }
}
// Add Observer 1
class Observer1 implements Observer
{
  public function update()
  {
    echo ' I'm the observer 1 I got your notice! I'm going to do what I have to do! <br/>\n';
  }
}
// Add Observer 2
class Observer2 implements Observer
{
  public function update()
  {
    echo ' I'm the observer 2 I got your notice! I'm going to do what I have to do! <br/>\n';
  }
}
// The test will begin next! 
$event = new Event();
// Add an observer to this event 
$event->addObserver(new Observer1);
$event->addObserver(new Observer2);
// Execute trigger Operation 
$event->trigger();
// It will be displayed at this time! 
// I'm going to start updating! 
// I'm the observer 1 I got your notice! I'm going to do what I have to do! 
// I'm the observer 2 I got your notice! I'm going to do what I have to do! 
?>

More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: