-
Notifications
You must be signed in to change notification settings - Fork 2
API Reference
Complete reference for every public class, interface, method, and
constant exported by initphp/events. Companion narrative pages are
linked where useful.
- Namespace:
InitPHP\Events\* - Composer package:
initphp/events - Source tree:
src/
The static facade. Defined in src/Events.php.
Detailed narrative: Events facade.
class Events
{
const PRIORITY_HIGH = Event::PRIORITY_HIGH; // 10
const PRIORITY_NORMAL = Event::PRIORITY_NORMAL; // 100
const PRIORITY_LOW = Event::PRIORITY_LOW; // 200
}Most calls are forwarded via __callStatic (and a mirrored __call)
to a lazily-constructed singleton Event
instance. Three lifecycle methods are defined on the facade itself:
getInstance, setInstance, and reset.
| Name | Value | Purpose |
|---|---|---|
PRIORITY_HIGH |
10 |
Semantic shorthand for "run early". |
PRIORITY_NORMAL |
100 |
Default for both Events::on() and EventEmitter::on(). |
PRIORITY_LOW |
200 |
Semantic shorthand for "run late". |
Ordering. See Listeners & Priorities — listeners are dispatched in ascending numeric priority order, with FIFO inside each priority bucket.
public static function on(string $name, callable $callback, int $priority = Event::PRIORITY_NORMAL): Event
public static function once(string $name, callable $callback, int $priority = Event::PRIORITY_NORMAL): Event
public static function off(string $name, callable $callback): Event
public static function removeAllListeners(?string $name = null): Event
public static function trigger(string $name, ...$arguments): bool
public static function setSimulate(bool $simulate = false): Event
public static function getSimulate(): bool
public static function setDebugMode(bool $debugMode = false): Event
public static function getDebugMode(): bool
public static function getDebug(): array
public static function clearDebug(): Event
public static function getEmitter(): EventEmitterEach is documented under Class: Event
below — Events simply exposes them statically.
public static function getInstance(): EventReturns the shared Event instance, lazily building one on first call.
public static function setInstance(Event $event): voidReplaces the shared instance. Useful for injecting a pre-configured dispatcher (e.g. one with simulate/debug already enabled, or a test double).
public static function reset(): voidDrops the shared instance so the next facade call rebuilds a fresh one. Call this in test setUp/tearDown and at the boundary of each request/job in long-running workers (queue workers, persistent HTTP servers).
The implementation behind the Events
facade. You can instantiate it directly if you want a dispatcher you
own (e.g. one per request in a long-running worker, or a test double).
Defined in src/Event.php.
final class Event
{
const PRIORITY_HIGH = 10;
const PRIORITY_NORMAL = 100;
const PRIORITY_LOW = 200;
}Internally, an Event owns a private EventEmitter instance and
delegates listener storage to it. The simulate/debug flags live on
Event itself.
public function __construct()Instantiates a fresh internal EventEmitter. Takes no arguments.
Registers a regular listener. Forwards to EventEmitter::on(). Throws
\InvalidArgumentException via the underlying emitter if $name is
not a string, $callback is not callable, or $priority is not an
integer.
Returns $this (chainable).
Registers a one-shot listener. Forwards to EventEmitter::once().
Dropped from the registry after the next trigger() of $name —
even if the chain was stopped by a false return, or a listener
threw (the cleanup runs in a try/finally block).
Returns $this. Same exception contract as on().
Removes a specific listener (regular or one-shot) for $name.
Forwards to EventEmitter::removeListener(). Listener identity is
strict array_search — same callable instance, not a
textually-equivalent rebuild.
Returns $this. Throws \InvalidArgumentException for invalid types.
Drops every listener (regular and one-shot) for $name, or every
listener for every event when $name is null. Forwards to
EventEmitter::removeAllListeners().
Returns $this. Throws \InvalidArgumentException if $name is
neither a string nor null.
Walks the listener list for $name (in priority order) and invokes
each one with the variadic $arguments unpacked positionally.
- Returns
trueif all listeners ran (or none were registered). - Returns
falseas soon as any listener returns strictfalse; the remaining listeners are skipped. - Records one row to the debug log per listener invocation when debug mode is on.
- Does not invoke listeners when simulate mode is on, but still records debug rows.
- One-shot listeners for
$nameare cleared at the end of the call regardless of whether the chain halted or a listener threw — the cleanup runs in atry/finallyblock. - Throws
\InvalidArgumentExceptionif$nameis not a string.
Toggle and read the simulate flag. Throws \InvalidArgumentException
if a non-boolean is passed. See
Simulate & Debug Mode.
Toggle and read the debug flag. Throws \InvalidArgumentException if
a non-boolean is passed. See
Simulate & Debug Mode.
Returns the in-memory debug log — a list of rows of the form
['start' => float, 'end' => float, 'event' => string]. One row per
listener invocation while debug mode was on. Returns a copy; mutating
it does not affect the underlying log.
Empties the debug log without disabling debug mode. Returns $this.
Returns the underlying EventEmitter instance — useful when callers
need the low-level emit() (no short-circuit on false) or
clearOnceListeners() on the same registry.
Hook for var_dump(). Returns:
[
'simulate' => bool,
'debugMode' => bool,
'debugData' => array,
]The internal EventEmitter and its listener registry are deliberately
not exposed in the debug payload — call getEmitter()->listeners()
explicitly if you want to inspect them.
The low-level emitter. Defined in
src/EventEmitter.php.
Implements EventEmitterInterface.
Detailed narrative: EventEmitter.
class EventEmitter implements EventEmitterInterface
{
// see methods below
}Registers a normal listener. Returns $this (chainable). Throws
\InvalidArgumentException on type mismatch.
Registers a one-shot listener. After the first emit($event), all
once listeners for that event are removed as a group. Returns
$this. Same validation as on().
Invokes every listener (both on and once) registered for $event,
in ascending priority order (FIFO within a priority bucket).
Arguments are unpacked positionally by call_user_func_array. Throws
\InvalidArgumentException if $event is not a string or
$arguments is not an array.
One-shot listeners for $event are removed after the call (only when
the call completes normally — exceptions propagate immediately without
the cleanup). Higher-level dispatchers that need "fire at most once
even on exception" semantics should call clearOnceListeners($event)
in their own try/finally block (see how Event::trigger() does it).
Returns void. Does not short-circuit on listeners returning
false — use the Events facade if you need that
contract.
Removes a specific listener from both the on and once queues for
$event. Matching uses strict equality (===); two distinct closure
objects with identical bodies are not equal. Throws on type mismatch.
No-op if the listener is not found.
With an event name, clears every listener (both queues) for that
event. With null, clears the entire registry — every event, every
listener. Throws \InvalidArgumentException if $event is provided
but is not a string.
Drops one-shot listeners without invoking them. With an event
name, clears only that event's once-listeners; with null, clears
every one-shot across every event. Regular on() listeners are not
touched.
Mostly used internally by higher-level dispatchers (the
Events facade's trigger() uses it in a
try/finally block to honour the once-contract on short-circuit or
exception). Throws \InvalidArgumentException if $event is provided
but is not a string.
Returns the current listener snapshot, sorted by priority (lower
numeric value first; FIFO within a priority bucket), already merged
across both the on and once registries.
-
listeners('user.login')— flat list of listeners for that event. -
listeners()— flat list of every listener across every event.
The returned array is a copy; mutating it does not affect the
registry. Iteration order matches the order emit() would invoke the
listeners.
Defined in src/EventEmitterInterface.php.
Implemented by EventEmitter.
interface EventEmitterInterface
{
public function on($event, $listener, $priority = 100);
public function once($event, $listener, $priority = 100);
public function emit($event, $arguments = []);
public function removeListener($event, $listener);
public function removeAllListeners($event = null);
public function clearOnceListeners($event = null);
public function listeners($event = null);
}The interface keeps PHP 5.6-compatible signatures (no scalar type hints, no return types) so it can be implemented on every supported PHP version. The concrete
EventEmitteradds the same type contracts at runtime via internal guards.
Use this type-hint in code that should accept either the bundled
EventEmitter or your own implementation:
final class OrderService
{
public function __construct(EventEmitterInterface $bus) { /* … */ }
}
clearOnceListeners()was added in 2.0. If you ship your own implementation of this interface, you must add this method — see Migration Guide.
Defined in src/aliases.php,
autoloaded via Composer's files directive. They map the legacy
InitPHP\EventEmitter\* namespace onto the new canonical classes:
class_alias(
\InitPHP\Events\EventEmitter::class,
'InitPHP\\EventEmitter\\EventEmitter'
);
class_alias(
\InitPHP\Events\EventEmitterInterface::class,
'InitPHP\\EventEmitter\\EventEmitterInterface'
);Both aliases are guarded by class_exists($name, false) /
interface_exists($name, false) to avoid duplicate-declaration fatals
if a stale copy of the deprecated initphp/event-emitter package
somehow still ships alongside.
Treat the aliases as a transition aid; see Migration Guide.
The library throws only built-in PHP exception types — there are no package-specific exception classes.
| Thrown by | Type | When |
|---|---|---|
Event::trigger() / Events::trigger()
|
\InvalidArgumentException |
$name is not a string. |
Event::on() / Event::once() (and the facade equivalents) |
\InvalidArgumentException |
$name not a string, $callback not callable, or $priority not an integer. |
Event::off() / Events::off()
|
\InvalidArgumentException |
$name not a string or $callback not callable. |
Event::removeAllListeners() / facade |
\InvalidArgumentException |
$name is neither a string nor null. |
Event::setSimulate() / setDebugMode() (and the facade equivalents) |
\InvalidArgumentException |
The flag value is not a boolean. |
EventEmitter::on() / once() / removeListener()
|
\InvalidArgumentException |
$event not a string, $listener not callable, or $priority not an integer. |
EventEmitter::emit() |
\InvalidArgumentException |
$event not a string, or $arguments not an array. |
EventEmitter::removeAllListeners() / listeners() / clearOnceListeners()
|
\InvalidArgumentException |
$event is provided but is not a string. |
Listeners themselves can throw anything; the package does not catch
listener exceptions — they propagate out of trigger() / emit() to
the caller. Event::trigger() runs its one-shot cleanup in a
try/finally, so a throwing listener still leaves the once-registry
in a clean state. EventEmitter::emit() does not — its
once-cleanup runs only on normal completion.
- Home — wiki entry point.
-
Eventsfacade — narrative reference for the static API. -
EventEmitter— narrative reference for the object-oriented API. - Listeners & Priorities — what a listener can be and how ordering works.
-
Stopping Propagation — the
return falsecontract onEvents::trigger(). - Simulate & Debug Mode — testing and timing.
- Migration Guide — what changed in 2.0 and what to do about it.
initphp/events · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core APIs
Practical
Reference