How to add events to a PHP project

Here is a short class that will let you add events to a PHP project.

The class EventManager defines a singleton, which you access with the EventManager::getInstance() expression.

Here is the logic:

  1. bind a handler to an event: EventManager::getInstance()->bind( ‘event’, ‘handler’ )
    An event can be a string or a PHP regular expression. A handler must be a PHP callable expression.
  2. trigger an event: EventManager::getInstance()->trigger( ‘event’ )
    A triggered event should be a string. That string is going to be matched against all the bound events. Whenever a match is found, all the handlers bound to the matching events will be called. All the arguments passed to trigger will be passed along to the handlers. If a handler returns a false value, all other handlers won’t be called.

{[ .EventManager | 1.hilite( =php= ) ]}


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.