PHPackages                             webfiori/event - 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. webfiori/event

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

webfiori/event
==============

A lightweight event dispatcher library for PHP.

v1.0.1(1mo ago)0665↓80.3%[1 issues](https://github.com/WebFiori/event/issues)1MITPHPPHP &gt;=8.1CI passing

Since May 29Pushed 1mo agoCompare

[ Source](https://github.com/WebFiori/event)[ Packagist](https://packagist.org/packages/webfiori/event)[ RSS](/packages/webfiori-event/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (4)Versions (5)Used By (1)

WebFiori Event
==============

[](#webfiori-event)

A lightweight event dispatcher library for PHP.

 [![](https://github.com/WebFiori/event/actions/workflows/php84.yaml/badge.svg?branch=main)](https://github.com/WebFiori/event/actions) [ ![](https://camo.githubusercontent.com/3a9e7a740a360c9c20e5e4eea795edcb14eacbb2427c568ad17a094df2108617/68747470733a2f2f636f6465636f762e696f2f67682f57656246696f72692f6576656e742f6272616e63682f6d61696e2f67726170682f62616467652e737667) ](https://codecov.io/gh/WebFiori/event) [ ![](https://camo.githubusercontent.com/e6a12db7b9d67395c01305f609caba46cf2be539f83ade55605736e739bbe0e3/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d57656246696f72695f6576656e74266d65747269633d616c6572745f737461747573) ](https://sonarcloud.io/dashboard?id=WebFiori_event) [ ![](https://camo.githubusercontent.com/449c2f7fd546a712a827a0b7825fb52a6667ed8d8a0164d661d6fe87aa360391/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f57656246696f72692f6576656e742e7376673f6c6162656c3d6c6174657374) ](https://github.com/WebFiori/event/releases) [ ![](https://camo.githubusercontent.com/90ec0f10402a66f7ba64a051981512a72bc83983c477c8b6719fa199c3cc28da/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77656266696f72692f6576656e743f636f6c6f723d6c696768742d677265656e) ](https://packagist.org/packages/webfiori/event)

Supported PHP Versions
----------------------

[](#supported-php-versions)

This library requires **PHP 8.1 or higher**.

Build Status[![](https://github.com/WebFiori/event/actions/workflows/php81.yaml/badge.svg?branch=main)](https://github.com/WebFiori/event/actions/workflows/php81.yaml)[![](https://github.com/WebFiori/event/actions/workflows/php82.yaml/badge.svg?branch=main)](https://github.com/WebFiori/event/actions/workflows/php82.yaml)[![](https://github.com/WebFiori/event/actions/workflows/php83.yaml/badge.svg?branch=main)](https://github.com/WebFiori/event/actions/workflows/php83.yaml)[![](https://github.com/WebFiori/event/actions/workflows/php84.yaml/badge.svg?branch=main)](https://github.com/WebFiori/event/actions/workflows/php84.yaml)[![](https://github.com/WebFiori/event/actions/workflows/php85.yaml/badge.svg?branch=main)](https://github.com/WebFiori/event/actions/workflows/php85.yaml)Features
--------

[](#features)

- **Simple API** — `listen()` and `dispatch()`, nothing else to learn
- **Callable listeners** — use closures for quick inline reactions
- **Class-based listeners** — implement `ListenerInterface` for reusable, testable handlers
- **Static facade** (`EventDispatcherFacade`) for quick usage without DI
- **Events are plain classes** — no interface or base class required
- **Zero dependencies** — requires only PHP 8.1+

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

[](#installation)

```
composer require webfiori/event
```

Usage
-----

[](#usage)

### Define Events

[](#define-events)

Events are plain classes that hold data about what happened:

```
class UserRegistered {
    public function __construct(
        public readonly string $email,
        public readonly string $name
    ) {}
}

class OrderPlaced {
    public function __construct(
        public readonly int $orderId,
        public readonly float $total
    ) {}
}
```

### Register Listeners

[](#register-listeners)

```
use WebFiori\Event\EventDispatcher;

$dispatcher = new EventDispatcher();

// Callable listener (inline)
$dispatcher->listen(UserRegistered::class, function (UserRegistered $event) {
    echo "Welcome {$event->name}!\n";
});

// Class-based listener (duck typing — full type hinting on handle())
class SendWelcomeEmail {
    public function handle(UserRegistered $event): void {
        mail($event->email, 'Welcome!', "Hi {$event->name}");
    }
}

$dispatcher->listen(UserRegistered::class, new SendWelcomeEmail());
```

### Dispatch Events

[](#dispatch-events)

```
$dispatcher->dispatch(new UserRegistered('john@example.com', 'John'));
// All listeners registered for UserRegistered are called
```

### Static Facade

[](#static-facade)

```
use WebFiori\Event\EventDispatcherFacade;

EventDispatcherFacade::listen(OrderPlaced::class, function (OrderPlaced $event) {
    echo "Order #{$event->orderId} placed!\n";
});

EventDispatcherFacade::dispatch(new OrderPlaced(42, 99.99));
```

API
---

[](#api)

### `EventDispatcher`

[](#eventdispatcher)

MethodDescription`listen(string $eventClass, callable|ListenerInterface $listener)`Register a listener for an event`dispatch(object $event)`Dispatch event to all registered listeners`getListeners(string $eventClass): array`Get listeners for an event`getListenerCount(): int`Total registered listeners`reset(): void`Remove all listeners### `EventDispatcherFacade`

[](#eventdispatcherfacade)

Static wrapper. Same methods as `EventDispatcher` plus `getInstance()`, `setInstance()`, `reset()`.

### Listener Classes (Duck Typing)

[](#listener-classes-duck-typing)

Any class with a public `handle()` method can be a listener. Type-hint the parameter with the specific event class for full IDE support:

```
class MyListener {
    public function handle(SomeEvent $event): void {
        // Full autocomplete on $event
    }
}
```

Design Decisions
----------------

[](#design-decisions)

- **Events are plain classes** — no marker interface needed. Any object can be an event.
- **Listeners use duck typing** — any class with a `handle()` method works. No interface to implement, full type hinting on the event parameter.
- **Listeners are registered explicitly** — `listen(EventClass, listener)`. No magic auto-discovery in the library (the framework handles that).
- **Listeners execute in registration order** — predictable, no priority system.
- **No event propagation stopping** — all listeners always run. Keep it simple.

License
-------

[](#license)

MIT

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance90

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% 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 ~4 days

Total

2

Last Release

52d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4c25e43acaa22b4fb758a710b69c2ab75947a6642925e3bec9c98196b1f2a433?d=identicon)[usernane](/maintainers/usernane)

---

Top Contributors

[![usernane](https://avatars.githubusercontent.com/u/12120015?v=4)](https://github.com/usernane "usernane (4 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/webfiori-event/health.svg)

```
[![Health](https://phpackages.com/badges/webfiori-event/health.svg)](https://phpackages.com/packages/webfiori-event)
```

###  Alternatives

[friends-of-hyva/magento2-preload-images

Preload images above the fold to improve performance metrics.

2318.4k](/packages/friends-of-hyva-magento2-preload-images)[seferov/composer-env-script

Composer script for handling gitignored env files

1141.5k](/packages/seferov-composer-env-script)

PHPackages © 2026

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