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

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

socarrat/events
===============

Simple event/hook system. Suited for all types of PHP projects.

v0.1.0(3y ago)1121MITPHPPHP &gt;=8.0CI passing

Since May 4Pushed 3y ago1 watchersCompare

[ Source](https://github.com/socarrat-php/events)[ Packagist](https://packagist.org/packages/socarrat/events)[ RSS](/packages/socarrat-events/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (1)

Socarrat Events
===============

[](#socarrat-events)

The Socarrat event library contains the base for your event dispatchers. It aims to be **quick** and **simple**, as well as **flexible** and **extensible** at the same time.

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

[](#installation)

You can install this package through [Composer](https://getcomposer.org/) by running `composer require socarrat/events`. The library will be available under the `Socarrat\Events` namespace.

Concept
-------

[](#concept)

The thing this package is all about, are *events*.

Imagine you're on a little boat at open sea, and you have no idea where you are. Luckily, you have some [flares](https://en.wikipedia.org/wiki/Flare#Military_use). They have been seen by a nearby ship, and you're rescued.

This getting-lost is the mehaphor for an *event*: some kind of situation arises. When you lightened the flares, you fired or *dispatched* the event. Thanks to hooks or *event listeners* that have been set, the smoke has been noticed. The action (rescue) is executed when the *callback* of the event listener (the person who had saw you calling for help) is executed.

Usage
-----

[](#usage)

### Creating an event

[](#creating-an-event)

In order to create an event, you need to extend the abstract `Event` class. It's important that you override the `$listeners` property.

For example:

```
use Socarrat\Events\Event;

class YourCustomEvent extends Event {
	static protected array $listeners;
}
```

Nothing more is needed! Of course, you can extend the event to have custom methods and properties.

### Listening to the event

[](#listening-to-the-event)

To register an event listener, you have to call the `on` method. The closure is called every time the event is dispatched.

The first parameter is the sort order. Hooks with a lower order are called earlier. This method returns the index that has been assigned to the hook. It may be higher than the index you provided. In that case, the previous index/es were alreaty occupied.

```
YourCustomEvent::on(1, function() {
	echo "Hi!";
});
```

### Dispatching the event

[](#dispatching-the-event)

Call the `dispatch` method to dispatch an event:

```
YourCustomEvent::dispatch();
```

The parameters you pass to this method are passed to all event listeners, so that you can provide context-specific information.

API
---

[](#api)

### `abstract class Socarrat\Events\Event`

[](#abstract-class-socarrateventsevent)

#### `static protected $listeners`

[](#static-protected-listeners)

An array of event listeners (closures) to call when the event is dispatched.

It's important that you override this in your own implementation!

#### `static function getName(): string`

[](#static-function-getname-string)

Returns the name of the event.

#### `static function on(int $priority, $callback): int`

[](#static-function-onint-priority-callback-int)

Registers an event listener for the current event. Returns the index that has been assigned to the listener.

Parameter nameTypeDefault valueDescription`$priority``int`-The priority of this listener.`$callback``closure`-The function to execute when the event is fired.#### `static function dispatch(...$callbackData)`

[](#static-function-dispatchcallbackdata)

Dispatches the event. This executes all registered callbacks in order, passing optional callback data to them.

**Warning:** this is a blocking function; it will stop execution until all callbacks have finished. Callbacks are executed synchronously.

Parameter nameTypeDefault valueDescription`...$callbackData``mixed`-Data to pass to the callbacks. Optional.Copyright
---------

[](#copyright)

(c) 2023 Romein van Buren. Licensed under the MIT license.

For the full copyright and license information, please view the [`license.md`](./license.md) file that was distributed with this source code.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

1106d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/80e830e4767cfe6a30503054d4b2c80b571bb5e8ec22ceddf28191e8a58f07f0?d=identicon)[Roman1001](/maintainers/Roman1001)

---

Top Contributors

[![garraflavatra](https://avatars.githubusercontent.com/u/34036573?v=4)](https://github.com/garraflavatra "garraflavatra (7 commits)")

---

Tags

event-dispatchereventseventeventshooksHOOK

### Embed Badge

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

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

###  Alternatives

[doctrine/event-manager

The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.

6.1k501.1M115](/packages/doctrine-event-manager)[tormjens/eventy

The WordPress filter/action system in Laravel

438912.9k16](/packages/tormjens-eventy)[laminas/laminas-eventmanager

Trigger and listen to events within a PHP application

1.0k69.8M225](/packages/laminas-laminas-eventmanager)[jbzoo/event

Library for event-based development

29760.0k5](/packages/jbzoo-event)[pagon/eventemitter

Event Emitter for PHP

2919.3k3](/packages/pagon-eventemitter)[zumba/symbiosis

Symbiosis, event structure for bootstrapping plugins.

1360.4k1](/packages/zumba-symbiosis)

PHPackages © 2026

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