wwaz/observer-php
Composer 安装命令:
composer require wwaz/observer-php
包简介
Lightweight, framework-agnostic Observer/EventBus implementation for PHP
README 文档
README
Lightweight, framework-agnostic implementation of the Observer pattern for PHP.
The package provides:
- a backward-compatible static API via
wwaz\Observer - an instance-based API via
wwaz\EventBusfor dependency injection
Requirements
- PHP
^8.1
Installation
composer require wwaz/observer-php
Quick examples
Static API
use wwaz\Observer; class Document { public function __construct() { Observer::subscribe($this, Button::class, 'click', function ($obj) { echo $obj->getName(); }); } } class Button { protected string $name = 'MyButton'; public function getName(): string { return $this->name; } public function click(): void { Observer::notify($this, 'click', null); } } $document = new Document(); (new Button())->click();
Instance-based API (dependency injection)
use wwaz\EventBus; class Document { public function __construct(private EventBus $eventBus) { $this->eventBus->subscribe($this, Button::class, 'click', function (Button $btn) { echo $btn->getName(); }); } } class Button { protected string $name = 'MyButton'; public function __construct(private EventBus $eventBus) {} public function getName(): string { return $this->name; } public function click(): void { $this->eventBus->notify($this, 'click', null); } } $eventBus = new EventBus(); $document = new Document($eventBus); $button = new Button($eventBus); $button->click();
API contract
All method signatures of Observer and EventBus remain backward-compatible.
subscribe()
Observer::subscribe(object $subscriber, string $observedName, string $action, callable $eventHandler): bool
- Registers a subscriber for one action, wildcard (
*) or comma-separated actions. - Duplicate subscriptions for the same subscriber/object/action triple are ignored.
- Returns
true.
notify()
Observer::notify(object|string $observed, string $action, mixed $data): bool
- Calls matching handlers immediately.
- Supports exact action match and wildcard subscriptions.
- Returns
falsewhen no subscriptions are registered for the observed class/object.
unsubscribe()
Observer::unsubscribe(object $subscriber, string $observedName, string $action): bool
- Removes subscriptions for one action or all (
*). - Returns
trueif at least one subscription was removed.
getSubscriptions()
Observer::getSubscriptions(?string $observedName = null): array|false
- Returns all subscriptions when no name is provided.
- Returns subscriptions for one observed name when provided.
- Returns
falsewhen no subscriptions exist for the given observed name.
Development
composer install
composer lint
composer stan
composer test
composer qa
Troubleshooting
- Run
composer validate --strictif Composer metadata changes. - If tests fail unexpectedly, clear local state and reinstall dependencies.
- Ensure your local PHP version matches the supported range.
Versioning and compatibility
- This package follows SemVer.
- Existing public API behavior is preserved in this modernization.
- New functionality should be additive and optional to avoid breaking changes.
wwaz/observer-php 适用场景与选型建议
wwaz/observer-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「events」 「observer」 「event-bus」 「pubsub」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wwaz/observer-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wwaz/observer-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wwaz/observer-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Библиотека для реализаций паттерна Pub/Sub
A collection of observer classes that can be use in your Laravel application.
Wireless Cross-Component Communication
Event dispatcher package
A powerful event calendar Tool for Laravel's Nova 5.
Command bus implementation: Commands and domain events
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-22