powertools / event
PHP PowerTools Event Component
dev-master / 1.0.x-dev
2016-04-07 18:07 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-12-11 19:04:52 UTC
README
PHPPowertools is a web application framework for PHP >= 5.4.
PHPPowertools/Event is the third component of the PHPPowertools that has been released to the public.
The purpose of this component is to provide a PHP-based event handling interface similar to both NodeJS's EventEmitter and your browser's EventTarget.
Example use :
use \PowerTools\Event_Emitter as Event_Emitter; // Define some event handlers $eventHandlers = [ 'handler1' => function($a = '', $b = '') { // var_dump($this); echo 'handler1 fired with parameters "' . $a . '" and "' . $b . '"<br />'; }, 'handler2' => function($a = '', $b = '') { // var_dump($this); echo 'handler2 fired with parameters "' . $a . '" and "' . $b . '"<br />'; }, 'handler3' => function($a = '', $b = '') { // var_dump($this); echo 'handler3 fired with parameters "' . $a . '" and "' . $b . '"<br />'; }, 'handler4' => function($a = '', $b = '') { // var_dump($this); echo 'handler4 fired with parameters "' . $a . '" and "' . $b . '"<br />'; } ]; // Create an instance of the Event_Emitter class $emitter = Event_Emitter::factory(); // Add your event handlers to your Event_Emitter instance for the 'go' event. $emitter->addListeners('go', $eventHandlers); // Emit the 'go' event, with parameters 'FOO' and 1. $emitter->emit('go', 'FOO', 1); // Remove the second event handler for the 'go' event $emitter->removeListener('go', $eventHandlers['handler2']); // Remove ALL listeners for the 'go' event $emitter->removeAllListeners('go');
Supported methods :
- addListener
- addListeners (1)
- once
- removeListener
- removeListeners (1)
- removeAllListeners
- setMaxListeners
- getMaxListeners
- listeners
- emit
- listenerCount
- dispatch
- These methods have no equivalent in either NodeJS's EventEmitter or your browser's EventTarget.