Library for event-based development

2.0.0 2019-11-24 16:08 UTC

This package is auto-updated.

Last update: 2020-05-26 23:58:56 UTC


PHP Library for event-based development

License Latest Stable Version Scrutinizer Code Quality

The EventEmitter is a simple pattern that allows you to create an object that emits events, and allow you to listen to those events.


# add to project
composer require jbzoo/event --update-no-dev
# via update
composer update jbzoo/event --no-dev

Simple example

require_once './vendor/autoload.php'; // composer autoload.php

// Get needed classes
use JBZoo\Event\EventManager;

$eManager = new EventManager();

// Simple
$eManager->on('create', function () {
    echo "Something action";

// Just do it!

Set priority

By supplying a priority, you are ensured that subscribers are handled in a specific order. The default priority is EventManager::MID. Anything below that will be triggered earlier, anything higher later. If there's two subscribers with the same priority, they will execute in an undefined, but deterministic order.

// Run it first
$eManager->on('create', function () {
    echo "Something high priority action";
}, EventManager::HIGH);

// Run it latest
$eManager->on('create', function () {
    echo "Something another action";
}, EventManager::LOW);

// Custom index
$eManager->on('create', function () {
    echo "Something action";
}, 42);

// Don't care...
$eManager->on('create', function () {
    echo "Something action";

Types of Callback

All default PHP callbacks are supported, so closures are not required.

$eManager->on('create', function(){ /* ... */ }); // Custom function
$eManager->on('create', 'myFunction');            // Custom function name
$eManager->on('create', ['myClass', 'myMethod']); // Static function
$eManager->on('create', [$object, 'Method']);     // Method of instance

Cancel queue of events

use JBZoo\Event\ExceptionStop;

$eManager->on('create', function () {
    throw new ExceptionStop('Some reason'); // Special exception for JBZoo/Event

$eManager->trigger('create'); // return 'Some reason' or TRUE if all events done

Passing arguments

Arguments can be passed as an array.

$eManager->on('create', function ($entityId) {
    echo "An entity with id ", $entityId, " just got created.\n";
$entityId = 5;
$eManager->trigger('create', [$entityId]);

Because you cannot really do anything with the return value of a listener, you can pass arguments by reference to communicate between listeners and back to the emitter.

$eManager->on('create', function ($entityId, &$warnings) {
    echo "An entity with id ", $entityId, " just got created.\n";
    $warnings[] = "Something bad may or may not have happened.\n";
$warnings = [];
$eManager->trigger('create', [$entityId, &$warnings]);


$eManager->on('item.*', function () {
    // item.init
    // item.save
    echo "Any actions with item";

$eManager->on('*.init', function () {
    // tag.init
    // item.init
    echo "Init any entity";

$eManager->on('*.save', function () {
    // tag.save
    // item.save
    echo "Saving any entity in system";

$eManager->on('*.save.after', function () {
    // tag.save.after
    // item.save.after
    echo "Any entity on after save";