benit8 / event-emitter
PHP event emitter with namespaces
v1.2.0
2023-03-28 13:53 UTC
Requires
- php: >=7.4
Requires (Dev)
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.10
This package is auto-updated.
Last update: 2024-12-29 12:32:35 UTC
README
An event emitter that supports namespaces.
Installing
$ composer require benit8/event-emitter
Usage
You can either extend the EventEmitter
class or use the EventEmitterTrait
. An interface
EventEmitterInterface
is also available.
use Benit8\EventEmitter\EventEmitterTrait; class MyReactor { use EventEmitterTrait; }
Reference
Adding listeners
$ev->on('user.created', function ($user) { // Fired every time a 'user.created' event is emitted }); $ev->on('user', function ($user) { // Listening to all 'user.*' events });
Adding one-shot listeners
$ev->once('user.created', function ($user) { // Will remove itself after firing });
Emitting events
$user = new User(/* ... */); // Will trigger 'user.created' and 'user' listeners $ev->emit('user.created', $user);
Removing a listener
$ev->removeListener('user.created', $myCallable);
Removing all listeners
// All listeners $ev->removeAllListeners(); // Subset listeners $ev->removeAllListeners('user');