PHPackages                             mario-legenda/php-event-emitter - 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. mario-legenda/php-event-emitter

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

mario-legenda/php-event-emitter
===============================

EventEmitter port from node to PHP

1.0.3(7y ago)09MITPHPPHP &gt;=7.0.0

Since Feb 28Pushed 7y ago1 watchersCompare

[ Source](https://github.com/MarioLegenda/php-event-emitter)[ Packagist](https://packagist.org/packages/mario-legenda/php-event-emitter)[ RSS](/packages/mario-legenda-php-event-emitter/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

Introduction
------------

[](#introduction)

This project is a light port of Nodes [EventEmitter class](https://nodejs.org/api/events.html). Although, it does not have all the features that Node EventEmitter has, it has all that you need to implement the Observer pattern right away in your classes with minimal code refactoring.

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

[](#installation)

Supports PHP &gt;7.0

`composer require mario-legenda/php-event-emitter`

#### Usage by extending the EventEmitter class

[](#usage-by-extending-the-eventemitter-class)

```
class Task extends EventEmitter
{
    public function __construct()
    {
        parent::__construct(); // don't forget to call the parent Event Emitter constructor

        $this->emit('classConstructed');
    }

    public function runTask(): ExtendedTask
    {
        $this->emit('preRunEvent');

        // some task specific code

        $this->emit('postRunEvent');

        $this->emit('taskFinished');

        return $this;
    }
}

$task = new Task();

$task
    ->runTask()
    ->on('classConstructed', function($val) {
        // called when the classConstructed event is emitted
    })
    ->on('preRunEvent', function($val) {
        // called when the preRunEvent event is emitted
    })
    ->on('postRunEvent', function($val) {
        // called when the postRunEvent event is emitted
    })
    ->on('taskFinished', function($val) {
        // called when the taskFinished event is emitted
    })

```

An alternative to calling EventEmitter::on() with a closure would be to create a class that implements the EventEmitter\\CallableInterface that exposes a single method `run()`. The drawback is that in PHP, we cannot set a variable number of parameters for the `run()` method. Therefor, if you want to use this interface, you would have to nullify every one of the arguments that you expect to receive. For example...

```

use EventEmitter\CallableInterface;

class Callable implements CallableInterface
{
    public function run($argumentOne = null, $argumentTwo = null)
    {
    }
}

```

#### Multiple event handlers

[](#multiple-event-handlers)

It is possible to chain multiple identical events

```
$eventEmitter = new EventEmitter();

$eventEmitter->emit('event', 'some value');

$eventEmitter
    ->on('event', function($val) {
        // called once
    })
    ->on('event', function($val) {
       // called twice
    })
    ->on('event', function($val) {
       // called three times
    })

```

#### Usage within an object

[](#usage-within-an-object)

```
class Task
{
    private $eventEmitter;

    public function __construct()
    {
        $this->eventEmitter = new EventEmitter();
    }

    public function runTask(): ExtendedTask
    {
        $this->eventEmitter->emit('preRunEvent');

        // some task specific code

        $this->eventEmitter->emit('postRunEvent');

        $this->eventEmitter->emit('taskFinished');

        return $this;
    }

    public function getEventEmitter(): EventEmitter
    {
        return $this->eventEmitter;
    }
}

$task
    ->runTask()
    ->getEventEmitter()
    ->on('classConstructed', function($val) {
        // called when the classConstructed event is emitted
    })
    ->on('preRunEvent', function($val) {
        // called when the preRunEvent event is emitted
    })
    ->on('postRunEvent', function($val) {
        // called when the postRunEvent event is emitted
    })
    ->on('taskFinished', function($val) {
        // called when the taskFinished event is emitted
    })

```

As you can see, the two examples are almost identical. Creating the EventEmitter instance inside the Task class is not proper dependency injection but it enables us to use the EventEmitter seamlessly. The client code does not now that Task is using (or is) an EventEmitter.

#### Exception handling

[](#exception-handling)

EventEmitter can handle internal exceptions thrown by your code.

```
$eventEmitter = new EventEmitter(true); // notice the argument. That means that exceptions will be handled by EventEmitter

$eventEmitter->emit('event', 'some value');

$eventEmitter
    ->on('event', function($val) {
        throw new \RuntimeException();
    })
    ->exception(function(\Throwable $e) {
        // $e instanceof RuntimeException
    })

```

If you chain multiple events, every event after the one where an exception occurred will not be called.

```
$eventEmitter
    ->on('event', function($val) {
        throw new \RuntimeException();
    })
    ->on('otherEvent', function($val) {
        // this event is never called. Execution passes to the exception()
    })
    ->exception(function(\Throwable $e) {
        // $e instanceof RuntimeException
    })

```

API
---

[](#api)

`EventEmitter::__construct($handleErrors = false)`

EventEmitter constructor. If provided `true` as the first argument, exceptions will be handled by the EventEmitter. Else, exceptions will be propagated to client code

`EventEmitter::emit(string $eventName, ...args): void`

Registers an event to be called with `EventEmitter::on()`. Must be called before `EventEmitter::on()`.

`EventEmitter::on(string $eventName, $callback): EventEmitter`

Executes an event. `$callback` has to be an instance of `\Closure` or a instance of `EventEmitter\CallableInterface`

`EventEmitter::exception($callback)`

Called only when `$handleError` argument is set to `true` in the constructor and an exception occurred in executing one of the events.

`EventEmitter::removeEvent(string $eventName, bool $strictCheck = false): bool`

Removes an event and returns `true` if successful or `false` on failure. If `$strictCheck` is true, an exception is throw if an event cannot be removed (if it doesn't exist for example).

`EventEmitter::removeAllEvents()`

Removes all events.

`EventEmitter::getEventNames(): ?array`

Returns an array of currently registered event names or `null` if there are none

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

2630d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2bbcc01eebe0796764e262a67b171cd2f871ce04169bcdbb180da41a39b0e5b8?d=identicon)[MarioLegenda](/maintainers/MarioLegenda)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mario-legenda-php-event-emitter/health.svg)

```
[![Health](https://phpackages.com/badges/mario-legenda-php-event-emitter/health.svg)](https://phpackages.com/packages/mario-legenda-php-event-emitter)
```

###  Alternatives

[unclecheese/betterbuttons

Adds new form actions and buttons to GridField detail form for usability enhancements.

85544.9k19](/packages/unclecheese-betterbuttons)

PHPackages © 2026

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