Api Documentation Version 0.9.2
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 helper functions available for this class, shipped with the ptc-helpers.php file. The class also supports event removal if needed. Event Propagation can also be prevented.
To start using the event dispatcher, simply include the class file, and start adding event listeners. Here's a quick and simple example:
To add event listeners, the method PtcEvent::listen( ) can be used. The following paragraphs will demostrate how event listeners can be added to your application.
The fastest way to add an event listener is using closures, have a look at the following example:
Declared functions can also be added as listeners:
Classes can also be added as event listeners, as shown in the next example:
By default, the PtcMapper tries to call the method
handle( ) from a class, to add a custom method use the
"*" are also accepted for the
This listener will be called every time an event with the prefix
"form" is called.
"$priority" can be used to control the events execution order:
To fire events, the method PtcEvent::fire( ) can be used:
To be able to manipulate data with the event listeners, the
"&" can be used to reference the
To prevent propagation, you can retun
"false" inside an event listener, and all events following that event will not be fired:
"$priority"argument to control execution order, refer to Assigning Priority to know more over this topic.Preventing Events Propagation
To retrieve previously added events, use the method PtcEvent:getEvents( );
"$name"is not used, all currently added events will be returned.
To remove event listeners the method PtcEvent::remove( ) can be used:
PhpToolCase library, provides helper functions for the PtcEvent component. To be able to use the class with the ptc-helpers.php functions, the component needs to be registered. Use the method PtcEvent::register( ) before calling any of the helper functions related with the PtcEvent component