roolith / event
PHP event listener
Installs: 27 648
Dependents: 1
Suggesters: 0
Security: 0
Stars: 15
Watchers: 3
Forks: 1
Open Issues: 0
Requires (Dev)
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-11-29 06:10:46 UTC
README
PHP event listener
Install
composer require roolith/event
Usage
Event::listen('login', function () { echo 'Event user login fired! <br>'; }); Event::trigger('login');
Working example
<?php use Roolith\Event\Event; require_once __DIR__ . '/PATH_TO_AUTOLOAD/autoload.php'; class User { public function login() { return true; } public function logout() { return true; } public function updated() { return true; } } Event::listen('login', function () { echo 'Event user login fired! <br>'; }); $user = new User(); if($user->login()) { Event::trigger('login'); }
Usage with param
Event::listen('logout', function ($param) { echo 'Event '. $param .' logout fired! <br>'; }); if($user->logout()) { Event::trigger('logout', 'user'); }
Usage with param array
Event::listen('updated', function($param1, $param2) { echo 'Event ('. $param1 .', '. $param2 .') updated fired! <br>'; }); if($user->updated()) { Event::trigger('updated', ['param1', 'param2']); }
Unregister an event
Event::unregister('updated');
Wildcard event
Event::listen('event.login', function () { echo 'Login Wild card fired! <br>'; }); Event::listen('event.logout', function () { echo 'Logout Wild card fired! <br>'; }); Event::listen('event.*', function ($param) { echo 'Wild card fired! - '.$param.' <br>'; }); Event::trigger('event.login', 'login'); Event::trigger('event.logout', 'logout');