PHP code example of coders / events

1. Go to this page and download the library: Download coders/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/ */

    

coders / events example snippets



Coders\Events\EventBus;
use Coders\Events\Event;

$eventBus = new EventBus();

// Simple listener
$eventBus->on('user.registered', function(Event $event) {
    echo "New user: " . $event->data['name'] . "\n";
});

// Listener with target
$eventBus->on('notification', function(Event $event) {
    echo "Email sent to: " . $event->data['email'] . "\n";
}, 'email');

$eventBus->on('notification', function(Event $event) {
    echo "SMS sent to: " . $event->data['phone'] . "\n";
}, 'sms');

// Simple event
$event = new Event('user.registered', [
    'name' => 'John Doe',
    'email' => '[email protected]'
]);
$eventBus->emit($event);

// Event with specific target
$emailEvent = new Event('notification', [
    'email' => '[email protected]',
    'message' => 'Welcome!'
], 'email');
$eventBus->emit($emailEvent);

new Event(string $type, mixed $data = null, ?string $target = null)


use Coders\Events\EventBus;
use Coders\Events\Event;

$eventBus = new EventBus();

// Register different notification methods
$eventBus->on('order.completed', function(Event $event) {
    $order = $event->data;
    echo "Sending email for order #{$order['id']}\n";
}, 'email');

$eventBus->on('order.completed', function(Event $event) {
    $order = $event->data;
    echo "Sending SMS for order #{$order['id']}\n";
}, 'sms');

$eventBus->on('order.completed', function(Event $event) {
    $order = $event->data;
    echo "Updating status for order #{$order['id']}\n";
}, 'status_update');

// Emit event
$order = ['id' => 12345, 'customer' => 'John Doe'];
$eventBus->emit(new Event('order.completed', $order));

// Send only email
$eventBus->emit(new Event('order.completed', $order, 'email'));


use Coders\Events\EventBus;
use Coders\Events\Event;

$eventBus = new EventBus();

// Different logging levels
$eventBus->on('log', function(Event $event) {
    $message = $event->data['message'];
    error_log("[ERROR] $message");
}, 'error');

$eventBus->on('log', function(Event $event) {
    $message = $event->data['message'];
    echo "[INFO] $message\n";
}, 'info');

$eventBus->on('log', function(Event $event) {
    $message = $event->data['message'];
    echo "[DEBUG] $message\n";
}, 'debug');

// Logging
$eventBus->emit(new Event('log', ['message' => 'Database connection error'], 'error'));
$eventBus->emit(new Event('log', ['message' => 'User logged in'], 'info'));


use Coders\Events\EventBus;
use Coders\Events\Event;

$eventBus = new EventBus();

$eventBus->on('calculate.sum', function(Event $event) {
    $numbers = $event->data;
    return array_sum($numbers);
});

$result = $eventBus->emit(new Event('calculate.sum', [1, 2, 3, 4, 5]));
echo "Sum: $result\n"; // Sum: 15

// Register listeners with different targets
$eventBus->on('process', $callback1, 'fast');
$eventBus->on('process', $callback2, 'slow');
$eventBus->on('process', $callback3); // no target

// Call only 'fast' listener
$eventBus->emit(new Event('process', $data, 'fast'));

// Call all listeners (when no target in event)
$eventBus->emit(new Event('process', $data));

$eventBus
    ->on('user.login', $loginCallback)
    ->on('user.logout', $logoutCallback)
    ->on('user.register', $registerCallback);