Luminova Framework

PHP Luminova: Managing and Emitting Custom Events Using Observers

Last updated: 2026-11-28 20:57:12

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(): self

Return:

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): int

Parameters:

ParameterTypeDescription
$namestringThe event name to trigger.
$argumentsarray<int\|string,mixed>Arguments to pass to each callback (default: []).
$hookstring\|nullOptional 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:


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): void

Parameters:

ParameterTypeDescription
$namestringThe event name to bind.
$hookstringA unique identifier for the callback.
$callbackcallableThe function to invoke when the event is triggered.

Return:

void - This method does not return a value.

Throws:


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): bool

Parameters:

ParameterTypeDescription
$namestringThe event name to unbind.
$hookstring\|nullOptional. 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:


events

Retrieve all event callbacks, or callbacks for a specific event.

public static function events(?string $name = null): array

Parameters:

ParameterTypeDescription
$namestring\|nullOptional. 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(): void

Return:

void - This method does not return a value.