EventableTrait
Implements events helpers.
Table of Contents
Methods
- off() : void
- Remove event listeners.
- on() : void
- Listen to an event.
- trigger() : array<string|int, mixed>
- Fire an event.
Methods
off()
Remove event listeners.
public
off([EventInterface>|null $event = null ]) : void
Specify null event to remove all listeners.
Parameters
- $event : EventInterface>|null = null
on()
Listen to an event.
public
on(EventInterface>|callable $event[, callable|null $fn = null ][, bool $append = true ]) : void
This will receive events from the instance class, and events from any parent class.
$this->on(function(MyEvent $event) });
Parameters
- $event : EventInterface>|callable
- $fn : callable|null = null
- $append : bool = true
Tags
trigger()
Fire an event.
protected
trigger(class-string|null $class, EventInterface $event[, bool $once = false ]) : array<string|int, mixed>
When calling this method, it's best to avoid self/static/get_class and explicitly declare which class is emitting the event.
Although self is reasonably predictable, there's plenty of potential
for confusion - so please be cautious.
// Good
this->trigger(MyClass::class, $event);
$this->trigger(self::class, $event);
// Beware
$this->trigger(static::class, $event);
$this->trigger(get_class($this), $event);
Parameters
- $class : class-string|null
- $event : EventInterface
- $once : bool = false
-
Don't trigger if the event has already run at least once.
Tags
Return values
array<string|int, mixed> —handler results