Event
in package
A class that offers simple events handling functionality (dispatching and listening).
Example:
// listening on an event
Event::listen('some.event', function ($arg1, $arg2) {
// do some stuff ...
});
// dispatching an event
Event::dispatch('some.event', [$arg1, $arg2]);
// check if an event has listeners
Event::hasListener('some.event');
// check if an event is dispatched
Event::isDispatch('some.event');
// get a registered or a new event object
Event::get('some.event');
// get a all registered events
Event::getRegisteredEvents();
Tags
Table of Contents
- $events : array<string|int, mixed>
- Here live all bindings.
- dispatch() : void
- Dispatches the passed event by executing all attached listeners and passes them the passed arguments.
- get() : object
- Returns an event object by its name or creates it if it does not exist.
- getRegisteredEvents() : array<string|int, object>
- Returns array of all registered events as an array `['event.name' => $eventObject, ...]`.
- hasListener() : bool
- Checks whether an event has any listeners or not.
- isDispatched() : bool
- Checks whether an event has already been dispatched or not.
- listen() : void
- Listens on the passed event and attaches the passed callback to it.
- create() : object
- Creates an event object and adds it to the registered events.
Properties
$events
Here live all bindings.
protected
static array<string|int, mixed>
$events
= []
Methods
dispatch()
Dispatches the passed event by executing all attached listeners and passes them the passed arguments.
public
static dispatch(string $event[, array<string|int, mixed> $arguments = null ][, object|null $callbackThis = null ]) : void
Parameters
- $event : string
-
Event name.
- $arguments : array<string|int, mixed> = null
-
[optional] Arguments array. Note that the arguments will be spread (
...$args
) on the callback and an additional argument of the event name will be appended to the arguments. - $callbackThis : object|null = null
-
[optional] The object the callback should be bound to.
Return values
void —get()
Returns an event object by its name or creates it if it does not exist.
public
static get(string $event) : object
The event object consists of the following properties:
-
name
: The event name. -
dispatched
: A boolean flag indicating whether the event has been dispatched or not. -
listeners
: An array of callbacks.
Parameters
- $event : string
-
Event name.
Tags
Return values
object —getRegisteredEvents()
Returns array of all registered events as an array `['event.name' => $eventObject, ...]`.
public
static getRegisteredEvents() : array<string|int, object>
Return values
array<string|int, object> —hasListener()
Checks whether an event has any listeners or not.
public
static hasListener(string $event) : bool
Parameters
- $event : string
-
Event name.
Tags
Return values
bool —isDispatched()
Checks whether an event has already been dispatched or not.
public
static isDispatched(string $event) : bool
Parameters
- $event : string
-
Event name.
Tags
Return values
bool —listen()
Listens on the passed event and attaches the passed callback to it.
public
static listen(string $event, callable $callback, int $priority) : void
Parameters
- $event : string
-
Event name.
- $callback : callable
-
A callback to process the event.
- $priority : int
-
[optional] The priority of the listener. Higher number means higher priority, numbers can be positive only (negative numbers are treated as zero). Zero (
0
) is reserved to add to the end of the stack (lowest priority).
Return values
void —create()
Creates an event object and adds it to the registered events.
protected
static create(string $event) : object
Parameters
- $event : string
-
Event name.