PHP code example of lithemod / events

1. Go to this page and download the library: Download lithemod/events library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lithemod / events example snippets


use Lithe\Events\Event;

$event = new Event('event.name', ['key' => 'value']);

use Lithe\Events\EventDispatcher;

$dispatcher = new EventDispatcher();

$listener = function ($data) {
    echo "Event data: " . json_encode($data);
};

// Register the listener
$dispatcher->on('event.name', $listener);

$event = new Event('event.name', ['key' => 'value']);
$dispatcher->emit($event);

$dispatcher->off('event.name', $listener);

use Lithe\Orbis\Events;

$listener = function ($data) {
    echo "Event data: " . json_encode($data);
};

// Register the listener
Events\on('event.name', $listener);

use Lithe\Orbis\Events;
use Lithe\Events\Event;

$event = new Event('event.name', ['key' => 'value']);
Events\emit($event);

Events\off('event.name', $listener);

use Lithe\Events\Event;
use Lithe\Orbis\Events;

// Create a listener
$listener = function ($data) {
    echo "Event received with data: " . json_encode($data) . "\n";
};

// Register the listener
Events\on('my.event', $listener);

// Emit the event
$data = ['msg' => 'Hello, world!'];
Events\emit(new Event('my.event', $data));

// Remove the listener
Events\off('my.event', $listener);