Libraries / event
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
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.
Regular listeners are invoked as $listener($payload).
Wildcard ("*") listeners are invoked as $listener($event, $payload).
Returns $this for method chaining.
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().
Returns $this for method chaining.
off(string $event, $listener): EventEmitter
Removes the first matching registration of $listener from $event.
Returns $this for method chaining.
emit(string $event, $payload = null): bool
Synchronously invokes all listeners registered for $event, followed by any wildcard ("*") listeners.
Returns true if at least one listener was invoked, or false otherwise.
Iterates over a snapshot of listeners so adding/removing listeners inside a callback is safe.
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
When $event is omitted or "", removes all listeners for all events.
When $event is provided, removes all listeners for that specific event.
Returns $this for method chaining.
Error Handling
All validation errors throw an Exception with a clear event: prefix:
event: event name must be a non-empty string — thrown by on, once, off, emit, listeners, listenerCount, and removeAllListeners (when a whitespace-only string is passed).
event: listener must not be null — thrown by on, once, and off when $listener is null.
event: cannot emit wildcard '*' directly — thrown by emit("*") to prevent ambiguous wildcard emission.
Examples
01 Quick 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.