Skip to content

ListenerProvider

Priority-ordered PSR-14 listener provider.

Listeners are registered against an event class name and are also matched for any subclass, implemented interface, or parent class of the dispatched event — so a listener on a base event (or an interface) sees every concrete subtype, the usual “listen broadly, dispatch specifically” behavior. Within a single event type, higher priority runs first; ties preserve registration order (stable).

final class ListenerProvider implements ListenerProviderInterface

ImplementsListenerProviderInterface
SourceEvent/ListenerProvider.php
MethodDescription
getListenersForEvent(object $event): iterable<callable>
hasListenersFor(string $eventClass): boolWhether any registered listener would fire for this event class (matching the same subclass/interface/parent rules ListenerProvider::getListenersForEvent() uses).
listen(string $eventClass, callable $listener, int $priority = 0): voidRegisters a listener for an event class, subclass or interface.
reset(): voidDrops every registered listener and both memoization caches.

public function getListenersForEvent(object $event): iterable<callable>

An event for which to return the relevant listeners.

ParameterTypeDescription
$eventobjectAn event for which to return the relevant listeners.

Returns iterable``<``callable``> — An iterable (array, iterator, or generator) of callables. Each callable MUST be type-compatible with $event.

public function hasListenersFor(string $eventClass): bool

Whether any registered listener would fire for this event class (matching the same subclass/interface/parent rules ListenerProvider::getListenersForEvent() uses).

Cheap gate for hot-path emit sites so a no-listener app pays only this lookup, not an event-object allocation. Memoized per event class: every Events::emit() call site pays this (~5-6 lifecycle emits per request), and typeChain() otherwise allocates two arrays (class_parents()/class_implements()) on every single call.

ParameterTypeDescription
$eventClassstring

Returns bool

public function listen(string $eventClass, callable $listener, int $priority = 0): void

Registers a listener for an event class, subclass or interface.

Higher $priority runs first; listeners registered at the same priority keep their registration order. Registering invalidates the memoized resolution and has-listeners caches, so listeners may be added at any point without stale lookups.

ParameterTypeDescription
$eventClassstring
$listenercallable
$priorityint

public function reset(): void

Drops every registered listener and both memoization caches.

The registration sequence counter restarts at zero, so ordering after a reset behaves as it would on a fresh provider. Intended for test isolation and for long-running workers that rebuild their listener set between runs.