wwaz/observer-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

wwaz/observer-php

最新稳定版本:v1.0.1

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\EventBus for 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 false when 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 true if 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 false when no subscriptions exist for the given observed name.

Development

composer install
composer lint
composer stan
composer test
composer qa

Troubleshooting

  • Run composer validate --strict if 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.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固