PHP Observer Pattern in Laravel Framework

不言
Release: 2023-04-01 17:16:01
Original
2022 people have browsed it

This article mainly introduces the PHP observer mode, and analyzes the simple definition and usage techniques of the PHP observer mode in the form of examples. Friends in need can refer to it

The examples in this article describe the PHP observer model. Share it with everyone for your reference, the details are as follows:

<?php
//观察者模式
//抽象主题类
interface Subject
{
  public function attach(Observer $Observer);
  public function detach(Observer $observer);
  //通知所有注册过的观察者对象
  public function notifyObservers();
}
//具体主题角色
class ConcreteSubject implements Subject
{
  private $_observers;
  public function __construct()
  {
    $this->_observers = array();
  }
  //增加一个观察者对象
  public function attach(Observer $observer)
  {
    return array_push($this->_observers,$observer);
  }
  //删除一个已经注册过的观察者对象
  public function detach(Observer $observer)
  {
    $index = array_search($observer,$this->_observers);
    if($index === false || !array_key_exists($index, $this->_observers)) return false;
    unset($this->_observers[$index]);
    return true;
  }
  //通知所有注册过的观察者
  public function notifyObservers()
  {
    if(!is_array($this->_observers)) return false;
    foreach($this->_observers as $observer)
    {
      $observer->update();
    }
    return true;
  }
}
//抽象观察者角色
interface Observer
{
  //更新方法
  public function update();
}
//观察者实现
class ConcreteObserver implements Observer
{
  private $_name;
  public function __construct($name)
  {
    $this->_name = $name;
  }
  //更新方法
  public function update()
  {
    echo &#39;Observer&#39;.$this->_name.&#39; has notify&#39;;
  }
}
$Subject = new ConcreteSubject();
//添加第一个观察者
$observer1 = new ConcreteObserver(&#39;baixiaoshi&#39;);
$Subject->attach($observer1);
echo &#39;the first notify:&#39;;
$Subject->notifyObservers();
//添加第二个观察者
$observer2 = new ConcreteObserver(&#39;hurong&#39;);
echo &#39;<br/>second notify:&#39;;
$Subject->attach($observer2);
/*echo $Subject->notifyObservers();
echo &#39;<br/>&#39;;
$Subject->notifyObservers();*/
?>
Copy after login

Running results:

the first notify:Observerbaixiaoshi has notify
second notify:

#The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How Laravel uses gulp to build front-end resources

How to implement automatic loading of composer in the Laravel framework

The above is the detailed content of PHP Observer Pattern in Laravel Framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template