PHPackages                             wwaz/observer-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wwaz/observer-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wwaz/observer-php
=================

Lightweight, framework-agnostic Observer/EventBus implementation for PHP

v1.0.1(1mo ago)01MITPHPPHP ^8.1CI passing

Since Sep 9Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/WWAZ/observer-php)[ Packagist](https://packagist.org/packages/wwaz/observer-php)[ RSS](/packages/wwaz-observer-php/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

Observer/EventBus for PHP
=========================

[](#observereventbus-for-php)

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
------------

[](#requirements)

- PHP `^8.1`

Installation
------------

[](#installation)

```
composer require wwaz/observer-php
```

Quick examples
--------------

[](#quick-examples)

### Static API

[](#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)

[](#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
------------

[](#api-contract)

All method signatures of `Observer` and `EventBus` remain backward-compatible.

### `subscribe()`

[](#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()`

[](#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()`

[](#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()`

[](#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
-----------

[](#development)

```
composer install
composer lint
composer stan
composer test
composer qa
```

Troubleshooting
---------------

[](#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
----------------------------

[](#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.

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance90

Actively maintained with recent releases

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~843 days

Total

3

Last Release

48d ago

Major Versions

0.0.1 → 1.0.02026-04-16

PHP version history (3 changes)0.0.1PHP ^7.3

1.0.0PHP &gt;=7.3

v1.0.1PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/25566288?v=4)[WWAZ](/maintainers/WWAZ)[@WWAZ](https://github.com/WWAZ)

---

Top Contributors

[![WWAZ](https://avatars.githubusercontent.com/u/25566288?v=4)](https://github.com/WWAZ "WWAZ (10 commits)")

---

Tags

phpeventsobserverevent buspubsub

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/wwaz-observer-php/health.svg)

```
[![Health](https://phpackages.com/badges/wwaz-observer-php/health.svg)](https://phpackages.com/packages/wwaz-observer-php)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
