PHP Luminova: Managing and Emitting Custom Events Using Observers
Register named callbacks, emit events, and remove listeners.
Luminova’s Event utility associates named events with callbacks. A hook name identifies each callback so the application can invoke or remove it individually. Registrations are shared within the current PHP process.
With the shared registry, you can:
- Bind callbacks to specific events.
- Trigger events and execute their associated callbacks.
- Remove specific callbacks or entire events.
- Retrieve all event callbacks.
- Clear all events.
Example
use Luminova\Utility\Event;
// Add a callback to an event
Event::on('user.register', 'sendWelcomeEmail', function($user) {
// Replace this with your application’s mailer call.
echo "Welcome, {$user->email}";
});
// Trigger the event
$user = (object) ['email' => '[email protected]'];
Event::emit('user.register', [$user]);
// Remove a specific callback
Event::off('user.register', 'sendWelcomeEmail');This example demonstrates how to bind, trigger, and remove callbacks from events using the Event class.
Advanced Usage
Event Binding Example
Group listener registration in an application class. Call its registration method once before emitting events.
Example
// app/Utils/Events.php
namespace App\Utils;
use Luminova\Utility\Event;
final class Events
{
public static function register(): void
{
Event::on('user.register', 'welcome', static function (object $user): void {
echo "Welcome, {$user->email}";
});
Event::on('file.upload', 'log', static function (string $filename): void {
echo "Uploaded {$filename}";
});
}
}use App\Utils\Events;
use Luminova\Utility\Event;
Events::register();
Event::emit('user.register', [(object) ['email' => '[email protected]']], hook: 'welcome');Class Definition
- Full namespace:
Luminova\Utility\Event - This class is final.
Methods
__construct
Initialize a new event class.
public function __construct()getInstance
Retrieves the singleton instance of the Event class.
public static function getInstance(): selfReturn:
self - The shared Event instance. Subsequent calls return the same instance.
emit
Triggers an event, executing all the callbacks associated with it. You can optionally target a specific hook within the event.
public static function emit(string $name, array $arguments = [], ?string $hook = null): intParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The event name to trigger. |
$arguments | array<int\|string,mixed> | Arguments to pass to each callback (default: []). |
$hook | string\|null | Optional hook name to trigger specific hooks (default: null). |
Return:
int - The number of callbacks invoked. A missing event or hook returns zero. Callbacks run synchronously and their exceptions propagate to the caller.
Throws:
- Luminova\Exceptions\InvalidArgumentException - Throws if an invalid event name was passed.
on
Binds a callback function to a specific event and hook. Registering the same event and hook again replaces the previous callback.
public static function on(string $name, string $hook, callable $callback): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The event name to bind. |
$hook | string | A unique identifier for the callback. |
$callback | callable | The function to invoke when the event is triggered. |
Return:
void - This method does not return a value.
Throws:
- InvalidArgumentException - Throws if an invalid event name, hook or callback was passed.
off
Removes a specific callback from an event or clears all callbacks if no hook is specified.
public static function off(string $name, ?string $hook = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The event name to unbind. |
$hook | string\|null | Optional. The unique identifier of the callback to remove (default: null). |
Return:
bool - Return true if successful, false if the event or callback does not exist.
Throws:
- InvalidArgumentException - Throws if an invalid event name was passed.
events
Retrieve all event callbacks, or callbacks for a specific event.
public static function events(?string $name = null): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string\|null | Optional. The event name to filter by (default: null). |
Return:
array<array-key, callable|array<array-key, callable>> - With a truthy event name, returns callbacks keyed by hook, or an empty array for an unknown event. With null, '', or '0', returns the full registry keyed by event name.
detach
Clears all events and their associated callbacks.
public static function detach(): voidReturn:
void - This method does not return a value.