Index

tEvent

MMO\core\classes · 1 symbol · generated 30 Sep 2026

trait

tEvent

tEvent.php:21

Trait giving a class (typically an "event" data object) its own static listener list and a way to broadcast an instance of itself to every registered listener, in ascending $order. One listener list per class: a subclass shares its parent's listeners (GetListeners merges them) but has its own separate AddListener().

trait MMO\core\classes\tEvent
Namespace
MMO\core\classes
class UserCreated { use tEvent; public function __construct(public readonly string $name) {} } UserCreated::AddListener(fn (UserCreated $e) => print("welcome {$e->name}\n")); UserCreated::Send('Marc'); // constructs `new UserCreated('Marc')` and dispatches it: prints "welcome Marc"

Index

Listeners static
AddListener($handler, $order) staticRegister a listener directly (as opposed to ConnectListeners's attribute-based discovery).
ConnectListeners($obj) staticScan $obj's methods for #[Listen(EventClass::class)] attributes and register each one as a listener on the matching event class.
DisconnectListeners($obj) staticUndo a previous ConnectListeners: removes every listener that connect had registered for $obj on this event class.
dispatch()Invoke every GetListeners listener with $this.
GetListeners() staticCollect every listener for this class, including those registered on its parent classes (so a subclass event is also caught by listeners of its parent event), sorted by ascending $order (ties keep …
HasListener($handler) static
RemoveListener($handler) staticRemove the last added handler from the listeners.
Send($args) staticConstruct a new instance of this class with $args, then dispatch it to every listener.
SetListenerOrder($handler, $order) staticChange the order of an already-registered listener, whether added directly via AddListener or indirectly via ConnectListeners (which also stores its [$obj, $methodName] callables here).

Properties1

Listeners

static
protected static array $Listeners = []
Type
array

Methods9

AddListener($handler, $order)

statictEvent.php:62

Register a listener directly (as opposed to ConnectListeners's attribute-based discovery).

static function AddListener(callable $handler, int $order = 0): void
$handlercallablecalled as $handler($eventInstance) when this event class is Send / dispatch-ed
$order = 0intlisteners run in ascending order (lower first); listeners with an equal order run in the order they were added
returnsvoid
UserCreated::AddListener(function (UserCreated $e) {...}, 10);

ConnectListeners($obj)

statictEvent.php:37

Scan $obj's methods for #[Listen(EventClass::class)] attributes and register each one as a listener on the matching event class. connect does the actual reflection/registration.

static function ConnectListeners(object|string $obj): void
$objobject | stringan instance (or class name, for static-only listener methods)
returnsvoid
throws
class UserService { #[Listen(UserCreated::class, order: 1)] public function onUserCreated(UserCreated $e): void {...} } UserCreated::ConnectListeners(new UserService()); // onUserCreated() is now called on every UserCreated::Send()

DisconnectListeners($obj)

statictEvent.php:49

Undo a previous ConnectListeners: removes every listener that connect had registered for $obj on this event class.

static function DisconnectListeners(object|string $obj): void
$objobject | string
returnsvoid
throws
UserCreated::DisconnectListeners($service); // $service's #[Listen] methods stop firing

dispatch()

tEvent.php:153

Invoke every GetListeners listener with $this. If the class also implements PSR-14's StoppableEventInterface, dispatch stops early as soon as isPropagationStopped() becomes true.

function dispatch(): static
returnsstatic$this, for chaining
(new UserCreated('Marc'))->dispatch(); // what Send() does internally

GetListeners()

statictEvent.php:123

Collect every listener for this class, including those registered on its parent classes (so a subclass event is also caught by listeners of its parent event), sorted by ascending $order (ties keep insertion order).

static function GetListeners(): array
returnsarray[[callable $handler, int $order], ...]
UserCreated::GetListeners(); // [[$handlerB, 5], [$handlerA, 10]] -- lower order first

HasListener($handler)

statictEvent.php:109
static function HasListener(callable|object $handler): bool
$handlercallable | objecteither the exact callable passed to AddListener, or (for a [$obj, $methodName] listener registered via ConnectListeners) just the object, to check whether any of its methods are currently listening
returnsbool
UserCreated::HasListener($handler); // true if $handler is currently registeredUserCreated::HasListener($service); // true if any of $service's methods were connected via #[Listen]

RemoveListener($handler)

statictEvent.php:91

Remove the last added handler from the listeners.

static function RemoveListener(callable $handler): boolNOTE: if you added the same handler multiple time, you need to call this function the same amount of times.
$handlercallable
returnsbooltrue if a listener was removed

Send($args)

statictEvent.php:143

Construct a new instance of this class with $args, then dispatch it to every listener.

static function Send(mixed ...$args): static
...$argsmixed
returnsstaticthe constructed (and dispatched) event instance
UserCreated::Send('Marc'); // equivalent to (new UserCreated('Marc'))->dispatch()

SetListenerOrder($handler, $order)

statictEvent.php:75

Change the order of an already-registered listener, whether added directly via AddListener or indirectly via ConnectListeners (which also stores its [$obj, $methodName] callables here).

static function SetListenerOrder(callable $handler, int $order): bool
$handlercallable
$orderint
returnsbooltrue if $handler was found and its order changed
UserCreated::SetListenerOrder($handler, 20); // moves $handler later in the dispatch order