Guides > Event Dispatcher

Introducing The Event Dispatcher Component

The PtcEvent component provides an observer implementation for your application, allowing the programmer to add and listen for events. Event execution order can be controlled with a priority parameter. The class can add closures and class methods as event listeners. Wildcards are also accepted to watch event listeners execution. There are also library helpers available for this class. The event dispatcher also supports event removal if needed. Event Propagation can also be prevented by returning false inside your event listeners.

Main Features:

  • Accepts closures and class methods as event listener
  • Supports wildcards for all added events
  • Library helpers can be added with the ptc-helpers.php file
  • Supports event execution order with the priority parameter
  • Supports Event removal if needed
  • Can Retrieve a list of added events
  • Supports event propagation prevention
  • Uses the PtcDebug class to log all event listeners execution

Getting Started

To start using the event dispatcher, download the file simply include the class file PtcEvent.php, and start adding event listeners. Here's a quick and simple example:

  1. require_once( 'PtcEvent.php' );
  3. // adding an event listener with a closure as callback
  4. PtcEvent::listen( 'form.submit' , function ( $data )
  5. {
  6.    // do some stuff
  7.     print "called event with closure as callback!";
  8. ) );
To fire event listeners, all we need to do is use the PtcEvent::fire( ) method:
  1. // firing an event
  2. PtcEvent::fire( 'form.submit' , array( 'some data') );
That should get you started with the Event Dispatcher component.
For a complete list of options and examples read the full user guide.

**If you like this class, help the project to grow by rating or writing a review at one of the following sites:

⇑Back to Top⇑