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

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

phpf/event
==========

Phpf Event package

06PHP

Since Jan 31Pushed 11y ago1 watchersCompare

[ Source](https://github.com/phpf/Event)[ Packagist](https://packagist.org/packages/phpf/event)[ RSS](/packages/phpf-event/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Event
=====

[](#event)

Simple, powerful, and extendable JavaScript-like events for PHP.

1. [Features](#features)
2. [Basic Usage](#basic-usage)
3. [Examples](#examples)

- [Priorities](#priorities)
- [Cancelling Events](#cancelling-events)
- [Single-Listener Events](#single-listener-events)
- [Returning Results](#returning-results)
- [Stopping Propagation](#stopping-propagation)
- [Retrieving Completed Events](#retrieving-completed-events)
- [Custom Event Objects](#custom-event-objects)

\##Features

- Simple syntax (`on()` to bind, `trigger()` to emit)
- Extendable event objects
- Priority ordering of callbacks
- Callbacks can be any callable (not limited to closures)
- An arbitrary number of arguments can be passed to callbacks
- Prevent default behavior (`preventDefault()`), or stop propagation altogether (`stopPropagation()`)
- One-time events (`one()`)
- Event cancelling (`off()`)

\##Basic Usage

```
$events = new \Phpf\Event\Manager;

$events->on('myevent', function ($event, $myarg) {

	if ($event->isDefaultPrevented()){
		return;
	}

	echo "I'm doing my event called $myarg!";
});

$events->trigger('myevent', 'Example'); // outputs "I'm doing my event called Example!"
```

\##Examples

\###Priorities

By default, events are added with a priority of 10 and *executed from lowest to highest*:

```
$events->on('myevent', function ($event) { echo "Child"; }, 15);

$events->on('myevent', function ($event) { echo "Bear"; }, 9);

$events->trigger('myevent'); // outputs "BearChild"
```

You can change the sort order to high-to-low like so:

```
$events->setSortOrder(\Phpf\Event\Manager::SORT_HIGH_LOW);
```

\###Cancelling Events

To cancel an event, simply call the `off()` method:

```
$events->off('myevent');
```

This will remove any listeners bound to the event, so they will not be called if subsequently triggered.

\###Single-Listener Events

You can limit an event's execution to a single listener by using the `one()` method instead of `on()`:

```
$events->one('myevent', function ($event) {
	echo "I will print.";
});

$events->on('myevent', function ($event) {
	echo "I will not print, even though I was bound later.";
});

$events->trigger('myevent'); // Prints "I will print."
```

\###Returning Results

When events listeners are executed, any value returned from the listener will be collected; on completion (or propagation stoppage), the results will be returned as an indexed array.

For example:

```
$events->on('myevent', function ($event) {

	return 'Hello';
});

$events->on('myevent', function ($event) {

	return 'Goodbye';
});

$results = $events->trigger('myevent');

print_r($results); // array(0 => 'Hello', 1 => 'Goodbye');
```

\###Stopping Propagation

Like JS, propagation of events can be stopped by a listener at any time.

```
$events->on('myevent', function ($event) {

	echo "This will be printed";

	$event->stopPropagation();
});

$events->on('myevent', function ($event) {

	echo "This will not be printed.";
});
```

```
$events->on('myevent', function ($event) {

	echo "I will not be called.";
}, 12);

$events->on('myevent', function ($event) {

	echo "I will print second.";

	$event->stopPropagation();
}, 11);

$events->on('myevent', function ($event) {

	echo "I will print first.";
});
```

\###Retrieving Completed Events

The completed events and their returned arrays are stored for later use. The event object can be retrieved using the `event()` method, the results using the `result()` method:

```
$results = $events->trigger('myevent');

// ...later on, in another script:
$myevent = $events->event('myevent');
$sameResults = $events->result('myevent');

// ... do stuff with event/results

// re-trigger the event !
$newResults = $events->trigger($myevent);
```

\###Custom Event Objects You can also pass an instance of the `Event` class to the `trigger()` method instead of the event ID. This way, you can use custom event objects.

For example, if you want listeners to be able to modify a single returned value (a "filter"), you could create a class like this:

```
namespace MyEvents;

use Phpf\Event\Event;

class FilterEvent extends Event {

	protected $value;

	public function __construct($id, $initial_value) {
		// must call parent constructor with id if overriding
		parent::__construct($id);
		$this->value = $initial_value;
	}

	public function getValue() {
		return $this->value;
	}

	public function setValue($value) {
		$this->value = $value;
	}
}
```

And then use it like so:

```
$events->on('myFilterEvent', function ($event) {
	$newval = rtrim($event->getValue(), 's') . ' objects';
	$event->setValue($newval);
});

$events->on('myFilterEvent', function ($event) {
	$event->setValue($event->getValue() . ' are cool.');
});

// Create new object with initial value "Custom events"
$filterEvent = new \MyEvents\FilterEvent('myFilterEvent', 'Custom events');

$events->trigger($filterEvent);

$filtered = $events->event('myFilterEvent');

echo $filtered->getValue(); // Prints "Custom event objects are cool."
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

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/2b5b7d38d1408fa7abf81c2650415faa6d135c0725b5a25c5e52aefeac03c455?d=identicon)[wells5609](/maintainers/wells5609)

---

Top Contributors

[![wells5609](https://avatars.githubusercontent.com/u/4088444?v=4)](https://github.com/wells5609 "wells5609 (24 commits)")

### Embed Badge

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

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

PHPackages © 2026

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