PHP Classes
elePHPant
Icontem

Events: Register and dispatch events

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2012-10-12 (2 years ago) RSS 2.0 feedNot yet rated by the usersTotal: 595 All time: 4,870 This week: 1,353Up
Version License PHP version Categories  
events 0.1.0GNU General Publi...5.4PHP 5, Language
Description Author  

This class can register and dispatch events.

It can register events that can be listened by adding callback functions or observer objects.

The class can also dispatch new events by calling the registered listener functions or objects.

Picture of AlexanderC
Name: AlexanderC is available for providing paid consulting. Contact AlexanderC .
Classes: 13 packages by
Country: Moldova Moldova
Age: 25
All time rank: 7741 in Moldova Moldova
Week rank: 129 Up1 in Moldova Moldova Equal
Innovation award
Innovation award
Nominee: 3x

Details provided by the author  
A simple but efficient way to deal with events.
[?] Why do we need events? 
[!] EDP(http://en.wikipedia.org/wiki/Event-driven_programming)- it's an must be in parralel computations, but also a good way to add callbacks to any system/kernel thing we have to deal with.

The PHP most important changes are already here: closures and traits helps us to make impossible things.

This small event library is intendet to offer an KIS-y and cleany way to handle all your events using advanced PHP 5.4 and higher features

Each event should be first registered, ex.:

    $eventsFactory = new Events\Factory::getInstance();
    $eventsFactory->addEvent('test');
    $eventsFactory->addEvent('test');
    $event = $eventsFactory->getEvent('test');

Now you can add event handlers. Basicaly every event is an object that holds all it's handlers, data and state.
In any time you can stop the event propagnation by calling $event->stopPropagnation(). It's an efficient way to stop propagnation
while PHP works in blocking mode. Observers are stored in an priority queue(used \SplPriorityQueue container).
Observer can be also provided without any priority and it will be called in an logic way by the object fire method.
Store an observer:

    $event->addObserver($callback); // first event registered in this way has priority 0, each after an decremented one[see __getMinDecrementValueBasedOnIniPrecision()]
    // or with priority
    $event->addObserver($callback, PHP_INT_MAX);

The event is fired very simple:

    $event->fire($data);
    // or even so
    $eventsFactory->fire($eventName, $data);

Event object can hold any property while they are generated by the Propertable trait methods(basicaly get|set used).
Initial provided data is stored in $data property of the event object.
  Files folder image Files  
File Role Description
Files folder imageTraits (2 files)
Plain text file Factory.php Class Main Events Factory
Plain text file Object.php Class Main Event Object
Accessible without login Plain text file README.md Data Auxiliary data

  Files folder image Files  /  Traits  
File Role Description
  Plain text file Propertable.php Class Class source
  Plain text file Singleton.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%Total:595All time:4,870
 This week:0This week:1,353Up