PHPackages                             ecfectus/events - 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. ecfectus/events

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

ecfectus/events
===============

PHP7 Event Dispatcher

024PHP

Since Oct 3Pushed 9y ago1 watchersCompare

[ Source](https://github.com/ecfectus/events)[ Packagist](https://packagist.org/packages/ecfectus/events)[ RSS](/packages/ecfectus-events/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Events
======

[](#events)

[![Build Status](https://camo.githubusercontent.com/932c2230b47270b078998f2b297bfdbc6c158609bd10364da7075ee4fc59c4fb/68747470733a2f2f7472617669732d63692e6f72672f65636665637475732f6576656e74732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ecfectus/events)

PHP7 Event dispatcher, utilizing new type hints, and the `Ds\PriorityQueue` data type for improved performance and reduced complexity.

Usage
-----

[](#usage)

Usage is simple, use `listen` to add a callback, and `fire` to run the event callbacks.

```
class TestEvent extends Ecfectus\Events\Event{
    public $value = [];
}

$dispatcher = new Ecfectus\Events\Dispatcher();

$dispatcher->listen(TestEvent::class, function(Event $e){
    $e->value[] = 2;
}, 500);

$dispatcher->listen(TestEvent::class, function(Event $e){
    $e->value[] = 1;
}, 1);

$result = $dispatcher->fire(new TestEvent()); $result->value will equal [2, 1]
```

Wildcard Listeners
------------------

[](#wildcard-listeners)

You can also add wildcard events, and priority for the events will be maintained.

```
$dispatcher->listen('TestEv*', function(Event $e){
    $e->value[] = 3;
}, 100);

$dispatcher->listen('*', function(Event $e){
    $e->value[] = 4;
}, -10);

$result = $dispatcher->fire(new TestEvent()); $result->value will equal [2, 3, 1, 4]
```

Callbacks
---------

[](#callbacks)

Callbacks can be anything `callable` by default, see the php docs here for details of whats available:

In addition you can set a resolver which will be called in the cases where the callback isnt callable, usefull for using containers to create objects.

For example supplying event listeners in a laravel style could be achieved like this:

```
$dispatcher->setResolver(function($callback = null){

    //return a function that can be invoked
    return function(Event $e) use ($callback){

        //parse the callback into something that can be used
        list($class, $method) = explode('@', $callback);
        $instance = $somecontainer->make($class);

        //return the result of the method
        return $instance->$method($event);
    };

});

$dispatcher->listen('*', 'MyClassName@handleEvent', -10);//MyClassName is created via the resolver and the result of the handleEvent method is returned.
```

Forgetting Events
-----------------

[](#forgetting-events)

You can remove all listeners for an event by calling the `forgot` dispatcher method.

```
$dispatcher->forget(TestEvent::class); all listeners for TestEvent will be forgotten
$dispatcher->forget('*'); all listeners for the wildcard * will be forgotten
```

Events
------

[](#events-1)

Events should extend the `Ecfectus\Events\Event` class, apart from that the events can be however you want to be.

The core event class simply provides a base to typehint the dispatcher from, you can include or add whatever functionality you want to the events.

In future we may add propagation features, or other yet to be decided features, instead of you having to change your code, we can add these to the core event class.

*Why not allow string event names?*

Well we pondered over this for a while, and we came to conclusion using the event name as the string representation of the class to be the best solution for multiple reasons.

1. Boilerplate code checking types and values for event names is reduced to a simple `class_name` call.
2. We know for sure what is being passed in and around the dispatcher, so we can type hint accordingly.
3. IDE and auto complete makes it much easier for developers than remembering string values.
4. In some cases addingin "flexibility" just adds confusion to documentation, confusion for users especially when working on projects where 1 developer prefers one style over another.

This way its very simple, you pass in an instance of your event, thats it.

Subscribers
-----------

[](#subscribers)

You can also create subscriber classes and add them to the dispatcher, these classes must provide a `subscribe` method and get passed an instance of the dispatcher.

Within the subscribers subscribe method you may use the dispatcher as normal, this is perfect for grouping event listeners.

```
class NameOfSubscriberClass{

    public function subscribe($dispatcher)
    {
        $dispatcher->listen('*', function(Event $e){
            $e->value[] = 1;
        });

        $dispatcher->listen('*', function(Event $e){
            $e->value[] = 2;
        });

        //more events
    }
}

$dispatcher->subscribe('NameOfSubscriberClass');// NameOfSubscriberClass will be created for you via (new NameOfSubscriberClass())

//or pass an instance (ideal if your subscriber needs constructor arguments)
$subscriber = new NameOfSubscriberClass();
$dispatcher->subscribe($subscriber);
```

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0b8fdb3cf22dac46100ad95eddb94f98b2a7a5002b29c4d2f812113f2867ff35?d=identicon)[leemason](/maintainers/leemason)

---

Top Contributors

[![leemason](https://avatars.githubusercontent.com/u/1238646?v=4)](https://github.com/leemason "leemason (12 commits)")

### Embed Badge

![Health badge](/badges/ecfectus-events/health.svg)

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

###  Alternatives

[phpactor/language-server

Generic Language Server Platform

56488.8k9](/packages/phpactor-language-server)

PHPackages © 2026

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