承接 phine/observer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phine/observer

最新稳定版本:2.0.1

Composer 安装命令:

composer require phine/observer

包简介

A PHP library that implements the observer pattern.

关键字:

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads

A PHP library that implements the observer pattern.

Summary

This library provides an implementation of the observer pattern. You can use it to create other libraries such as event managers, state machines, MVC frameworks, and even provide a plugin system for application.

Requirement

Installation

Via Composer:

$ composer require "phine/observer=~2.0" 

Usage

To create a subject, you will need to either create your own implementation of SubjectInterface, or use the bundled Subject class.

use Phine\Observer\Subject; $subject = new Subject();

Observing

You will then need to create your own implementation of ObserverInterface to observe changes made to the subject. You may use multiple instances of the observer implementation, or even the same instance multiple times.

use Phine\Observer\ObserverInterface; use Phine\Observer\SubjectInterface; // register a few instances $subject->registerObserver(new Message('First')); $subject->registerObserver(new Message('Second')); // register the same one twice $reuse = new Message('Third'); $subject->registerObserver($reuse); $subject->registerObserver($reuse); // notify all observers of an update $subject->notifyObservers(); /**  * Simply echos a message when updated.  */ class Message implements ObserverInterface { /**  * The message to echo.  *  * @var string  */ private $message; /**  * Sets the message to echo on update.  *  * @param string $message The message.  */ public function __construct($message) { $this->message = $message; } /**  * {@inheritDoc}  */ public function receiveUpdate(SubjectInterface $subject) { echo $this->message, "\n"; } }

With the example above, you can expect the following output:

First Second Third Third 

Prioritizing Observers

Implementations of SubjectInterface support prioritizing observers during registration (registerObserver()). By default, shown in all the examples provided, the priority is SubjectInterface::FIRST_PRIORITY (which is 0, zero). You may, however, specify your own priority:

$subject->registerObserver(new Message('A'), SubjectInterface::LAST_PRIORITY); $subject->registerObserver(new Message('B'), 789); $subject->registerObserver(new Message('C'), 123); $subject->registerObserver(new Message('D'), 456); $subject->registerObserver(new Message('E'), SubjectInterface::FIRST_PRIORITY);

With the above example, you can expect the following output:

E C D B A 

When a subject updates its observers it begins at priority 0 (zero), and works its way to PHP_INT_MAX (the lowest possible priority). If multiple observers are registered using the same provider, they will be updated in the order that they were registered.

Interrupting an Update

When a subject is in the process of updating its registered observers, an observer may interrupt the subject. An interrupt is performed by an observer when it calls the SubjectInterface::interruptUpdate() method.

use Phine\Observer\Exception\ReasonException; /**  * Simply interrupts the subject in the middle of an update.  */ class InterruptingCow implements ObserverInterface { /**  * Interrupts the update.  */ public function receiveUpdate(SubjectInterface $subject) { // do some work $subject->interruptUpdate( new ReasonException('MOOOOO') ); // do some final work } }

Using the following example:

// create a new subject $subject = new Subject(); // register some observers $subject->registerObserver(new Message('So what did the interrupt cow say?')); $subject->registerObserver(new InterruptingCow()); $subject->registerObserver(new Message('We never get this far.')); // notify the observers $subject->notifyObservers();

You can expect the following output:

So what did the interrupting cow say? PHP Fatal error: Uncaught exception '[...]' with message 'MOOOOO' [...] [...] 

Observers are not required to provide a reason (instance of ReasonException), but it will definitely help during the debugging process if one is given.

Collections of Subjects

There may be occasions where you will need to manage a collection of subjects. The library provides two ways of doing so: Collection and ArrayCollection. The Collection will associate an individual subject with a specific unique identifier.

use Phine\Observer\Collection; // create a new collection $collection = new Collection(); // register a few subjects $collection->registerSubject('one', new Subject()); $collection->registerSubject('two', new Subject()); $collection->registerSubject('three', new Subject());

You can then retrieve the subjects or replace them as needed.

// replace one $collection->registerSubject('two', new Subject()); // update observers of another $collection->getSubject('three')->notifyObservers();

The ArrayCollection class provides a leaner way of managing subject registrations. It is an extension of the Collection class that supports array access through ArrayCollectionInterface.

use Phine\Observer\ArrayCollection; // create a new collection $collection = new ArrayCollection(); // register a few subjects $collection['one'] = new Subject(); $collection['two'] = new Subject(); $collection['three'] = new Subject();

Like the regular Collection class, you can also replace and retrieve individual subjects.

// replace one $collection['two'] = new Subject(); // update observers of another $collection['three']->notifyObservers();

Documentation

You can find the API documentation here.

License

This library is available under the MIT license.

phine/observer 适用场景与选型建议

phine/observer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 171.67k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「observer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phine/observer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phine/observer 我们能提供哪些服务?
定制开发 / 二次开发

基于 phine/observer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 171.67k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 15
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04