Every public symbol in the package, by class.
Forwards every static call to a shared Event instance via
__callStatic. See chapter 2 for context.
const PRIORITY_LOW = 200;
const PRIORITY_NORMAL = 100;
const PRIORITY_HIGH = 10;These mirror Event::PRIORITY_*. Lower numeric value runs first.
public static function getInstance(): EventReturn the shared Event instance, building one on first call.
public static function setInstance(Event $event): voidReplace the shared instance. Useful for injecting a pre-configured dispatcher (e.g. with simulate or debug already enabled) before the rest of your code touches the facade.
public static function reset(): voidDrop 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.
Every method that exists on Event is reachable through Events
via __callStatic. The most common ones are listed here for
discoverability, but the source of truth is Event (next section).
public static function trigger(string $name, ...$arguments): bool
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 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(): EventEmitterBuilt on top of EventEmitter. Adds priority-ordered dispatch with
"return false stops the chain" semantics, plus opt-in simulate
and debug modes.
const PRIORITY_LOW = 200;
const PRIORITY_NORMAL = 100;
const PRIORITY_HIGH = 10;public function __construct()Builds an EventEmitter internally. No arguments.
public function on(string $name, callable $callback, int $priority = self::PRIORITY_NORMAL): selfRegister a regular listener. Returns $this for chaining.
- Throws
\InvalidArgumentExceptionif$nameis not a string,$callbackis not callable, or$priorityis not an integer. - The underlying
EventEmitterfolds$nameto lower-case.
public function once(string $name, callable $callback, int $priority = self::PRIORITY_NORMAL): selfRegister a one-shot listener — dropped after the next trigger()
of $name. Same exception contract as on().
public function off(string $name, callable $callback): selfRemove 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.
public function removeAllListeners(?string $name = null): selfDrop every listener for $name, or every listener for every event
when $name is null. Throws \InvalidArgumentException if $name
is neither a string nor null.
public function trigger(string $name, ...$arguments): boolDispatch $name — invoke every registered listener in ascending
priority order, forwarding ...$arguments to each via
call_user_func_array.
Returns:
trueif every listener ran to completion without returningfalse,falseif any listener returned booleanfalse(the chain is halted at that point — subsequent listeners are not invoked).
One-shot listeners are dropped after the dispatch, regardless of
whether the chain was halted or a listener threw. The cleanup is in
a try/finally block.
Throws \InvalidArgumentException if $name is not a string.
public function setSimulate(bool $simulate = false): self
public function getSimulate(): boolWhen true, trigger() walks the priority queue but does not
invoke the listeners — and always returns true. Setter throws
\InvalidArgumentException if $simulate is not a boolean.
public function setDebugMode(bool $debugMode = false): self
public function getDebugMode(): bool
public function getDebug(): array
public function clearDebug(): selfWhen setDebugMode(true), every listener invocation appends an
entry to an internal log:
['start' => float, 'end' => float, 'event' => string]start is captured via microtime(true) before the listener
runs; end is captured after it returns (or throws — though the
throw propagates).
getDebug() returns the log as a plain array.
clearDebug() empties the log and returns $this.
The log is not capped automatically — long-running workers with
debug mode on should call clearDebug() periodically.
public function getEmitter(): EventEmitterReturns the underlying EventEmitter. Useful when you need both
the high-level trigger() and the low-level emit() /
clearOnceListeners() on the same listener registry.
public function __debugInfo(): arrayReturns ['simulate' => bool, 'debugMode' => bool, 'debugData' => array]
— a var_dump-friendly snapshot. Does not include the raw listener
registry; reach into getEmitter()->listeners() if you need that.
Plain on / once / emit / removeListener event emitter.
Implements EventEmitterInterface. No simulate, no debug, no
short-circuit on false.
public function on(string $event, callable $listener, int $priority = 100): self
public function once(string $event, callable $listener, int $priority = 100): selfRegister a listener. on() is permanent; once() is one-shot
(dropped after the next emit() or clearOnceListeners()).
Both throw \InvalidArgumentException for type-incorrect arguments
(non-string event, non-callable listener, non-int priority).
Both return $this.
public function emit(string $event, array $arguments = []): voidDispatch $event. Listeners are invoked in ascending priority
order; within a priority, FIFO by registration order. $arguments
is unpacked and forwarded to each listener via call_user_func_array.
Once-listeners for $event are dropped after the dispatch.
Throws \InvalidArgumentException if $event is not a string or
$arguments is not an array.
public function removeListener(string $event, callable $listener): voidRemove every occurrence of $listener for $event, across both
the regular and one-shot registries and across every priority slot.
Throws \InvalidArgumentException for non-string event / non-callable
listener.
public function removeAllListeners(?string $event = null): voidWipe one event's listeners (both regular and one-shot), or every
listener for every event when $event is null.
public function clearOnceListeners(?string $event = null): voidDrop one-shot listeners without invoking them. Used internally by
higher-level dispatchers (Event::trigger()) that run listeners
themselves and need to honour the once-contract.
public function listeners(?string $event = null): arrayReturn the listeners for $event (or for every event, if null),
already merged across both registries and sorted by priority.
Useful for diagnostics.
The integer literal 100 — equivalent to Event::PRIORITY_NORMAL.
The named constants live on Event, not on EventEmitter.
The contract EventEmitter implements. If you ship your own
implementation, your class must define:
public function on($event, $listener, $priority = 100);
public function once($event, $listener, $priority = 100);
public function removeListener($event, $listener);
public function removeAllListeners($event = null);
public function listeners($event = null);
public function emit($event, $arguments = []);
public function clearOnceListeners($event = null);clearOnceListeners() was added in 2.0 — it is a BC break for
anyone shipping their own implementation.
src/aliases.php (autoloaded by Composer via the files autoload
entry) registers:
class_alias('InitPHP\\Events\\EventEmitter', 'InitPHP\\EventEmitter\\EventEmitter');
class_alias('InitPHP\\Events\\EventEmitterInterface', 'InitPHP\\EventEmitter\\EventEmitterInterface');So code written against the deprecated initphp/event-emitter
package keeps working unchanged. See
chapter 7.
Every type-validation error in the package raises
\InvalidArgumentException. The package does not define its own
exception types.
| Method | Raises \InvalidArgumentException when |
|---|---|
Event::trigger() |
$name is not a string. |
Event::on() / Event::once() |
$name is not a string, $callback is not callable, or $priority is not an integer. |
Event::off() |
$name is not a string or $callback is not callable. |
Event::removeAllListeners() |
$name is neither a string nor null. |
Event::setSimulate() |
$simulate is not a boolean. |
Event::setDebugMode() |
$debugMode is not a boolean. |
EventEmitter::on() / EventEmitter::once() |
Non-string event, non-callable listener, or non-int priority. |
EventEmitter::emit() |
Non-string event, or $arguments is not an array. |
EventEmitter::removeListener() |
Non-string event or non-callable listener. |
EventEmitter::removeAllListeners() / clearOnceListeners() |
$event is neither a string nor null. |
EventEmitter::listeners() |
$event is neither a string nor null. |
The package does not throw any other exception type on its own —
exceptions propagated from listeners of course pass through
untouched. (trigger() still cleans up once-listeners on the way
out via try/finally.)