1. Go to this page and download the library: Download phossa/phossa-event 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/ */
phossa / phossa-event example snippets
$dispatcher = new EventDispatcher();
// bind event 'user.login' to a callable
$dispatcher->on('user.login', function(Event $evt) {
// ...
});
// trigger the 'user.login' event, passing data
$dispatcher->trigger('user.login', [ 'user' => $user ]);
// run this after script ends
$dispatcher->ready(function() {
// ...
});
use Phossa\Event\Interfaces;
class MyListener implements Interfaces\EventListenerInteface
{
/*
* Get events and callables MyListener listens to
*/
public function getEventsListening()
{
return array(
'eventName1' => 'method1', // method1 of $this
'eventName2' => array('method2', 20), // priority 20
'eventName3' => array( // multiple callables
[ 'method3', 70 ],
[ 'method4', 50 ]
)
);
}
}
$listener = new MyListener();
// create an event manager
$evtManager = new Event\EventManager();
// attach a listener object
$evtManager->attachListener($listener);
// attach a callable directly to 'oneSpecialEvent'
$callable = function(Event\Event $evt) {
...
};
$evtManager->attachListener($callable, 'oneSpecialEvent');
// detach a callable
$evtManager->detachListener($callable);
use Phossa\Event\Interfaces;
class MyEventAware implements Interfaces\EventAwareInterface
{
// add setEventManager() and triggerEvent()
use Interfaces\EventAwareTrait;
...
}
$subject = new MyEventAware();
// set manager to event-aware subject
$subject->setEventManager($evtManager);
// trigger event
$subject->triggerEvent('eventName2');
use Phossa\Event\Interfaces;
class StaticListener implements Interfaces\EventListenerStaticInteface
{
/*
* Get events and callables StaticListener listens to
*/
public static function getEventsListening()
{
return array(
'eventName1' => 'method1', // method1 of $this
'eventName2' => array('method2', 20), // priority 20
'eventName3' => array( // multiple callables
[ 'method3', 70 ],
[ 'method4', 50 ]
)
);
}
}
use Phossa\Event\Interfaces;
class StaticEventAware implements Interfaces\EventAwareStaticInterface
{
// add setEventManager() and triggerEvent()
use Interfaces\EventAwareStaticTrait;
...
}
// create an event manager/dispatcher
$evtManager = new Event\EventManager();
// attach a static listener class
$evtManager->attachListener(StaticListener::CLASS);
// set manager/dispatcher to event-aware static subject class
StaticEventAware::setEventManagerStatically($evtManager);
// trigger event by the static subject class
StaticEventAware::triggerEventStatically('eventName2');
use Phossa\Event;
// global event manager
$global_manager = new Event\EventManager();
$global_manager->attachListener($some_global_event_listener);
// local event manager
$local_manager = new Event\Variation\EventManagerComposite();
$local_manager->attachListener($local_listener);
// allow local event manager dispatch events to global event manager
$local_manager->setOtherManager('global', $global_manager);
// the event aware subject
$subject = new MyEventAware();
// set event manager
$subject->setEventManager($local_manager);
// fire up an event, will look into event handling queue from both
// $local_manager and $global_manager
$subject->triggerEvent('some_event');
use Phossa\Event;
// low-level manager to hide
$_evtManager = new EventManager();
$_evtManager->attachListener(...);
...
// expose an immutable event managet to user
$evtManager = new Variation\ImmutableEventManager($_evtManager);
// cause an exception
$evtManager->detachListener( ... );
// get global copy by using static method `getInstance()`
$globalEventManager = ShareableEventManager::getInstance();
// normal event managers
$localEventManager = new ShareableEventManager();
// is this the global copy?
if ($evtManager->isShareable()) {
...
} else {
...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.