event

EventEmitter / pub-sub event bus with once and wildcard listeners.

v1.0.0Application & logicbundledimport "event";
Overview Installation Documentation Examples

Overview

A zero-dependency EventEmitter / Pub-Sub event bus library for HolyPHP. Supports standard listeners (on), one-time listeners (once), listener removal (off, removeAllListeners), wildcard (*) listeners, and clear validation errors.

bundled — works offline zero dependencies pubsub emitter

Quick start

import "event";

$bus = event_emitter();

$bus->on("user.signup", function ($user) {
    println("Welcome, " . $user["name"] . "!");
});

$bus->once("app.ready", function ($info) {
    println("App initialized: " . $info);
});

$bus->on("*", function ($event, $payload) {
    println("[audit] event=" . $event);
});

$bus->emit("app.ready", "v1.0.0");
$bus->emit("user.signup", ["name" => "Alice"]);

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install event

This adds the library to hphp.yaml, installs it into .hphp/packages/event/ and pins the exact version in hphp.lock:

require:
  event: "^1.0"

Use it

import "event";

event is bundled with the compiler, so import "event"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install event          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install event
Packageevent
Version1.0.0 (follows the compiler)
Entry fileevent.hphp
Sourcepackages/lib/event ↗

Documentation

API Reference

Helper Function

event_emitter(): EventEmitter

Creates and returns a new EventEmitter instance.

---

EventEmitter Class

on(string $event, $listener): EventEmitter

Registers a persistent listener for $event. Use "*" as the event name to register a wildcard listener that receives all emitted events.

once(string $event, $listener): EventEmitter

Registers a one-time listener for $event (or "*"). The listener is automatically removed before it is invoked on the next matching emit().

off(string $event, $listener): EventEmitter

Removes the first matching registration of $listener from $event.

emit(string $event, $payload = null): bool

Synchronously invokes all listeners registered for $event, followed by any wildcard ("*") listeners.

listeners(string $event): array

Returns an array of all listener callbacks currently registered for $event.

listenerCount(string $event): int

Returns the number of listeners currently registered for $event.

eventNames(): array

Returns an array of all event names that currently have at least one registered listener.

removeAllListeners(string $event = ""): EventEmitter

Error Handling

All validation errors throw an Exception with a clear event: prefix:

Examples

01Quick start

Bundled with the compiler — import "event"; works offline in any file, or add it to a project with hphp install event.

import "event";

$bus = event_emitter();

$bus->on("user.signup", function ($user) {
    println("Welcome, " . $user["name"] . "!");
});

$bus->once("app.ready", function ($info) {
    println("App initialized: " . $info);
});

$bus->on("*", function ($event, $payload) {
    println("[audit] event=" . $event);
});

$bus->emit("app.ready", "v1.0.0");
$bus->emit("user.signup", ["name" => "Alice"]);
Try it
hphp install event
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_event.hphp.