Minor "PHP framework" 5. Events, minorphp framework Events
Events Event 5.1
The minor Event class provides a simple observer implementation that allows you to subscribe and listen to events in your application.
5.1.1 Subscribing to Events
First create an event class:
Phpnamespace app\event; Use minor\event\event; class extends event{ private$name; Public function __construct ($name) { $this$name; } Public function setName ($name) { $this$name; } publicfunction getName () { return$this- >name;} }
Then register this event in the configuration file:
PHPreturn$events = [ ' app\event\demoevent ' = [' App\ Listener\demolistener ' = ' handle ', ],;
5.1.2 Triggering events
Minor provides an event management class: Minor\event\eventmanger, which can trigger this event by invoking the static method Fire:eventmanager::fire ($event) of the class, for example:
class extends controller{ publicfunction Bar ($productName) { $eventnew demoevent (' demoevent '); EventManager:: Fire ($event); ... }}
5.2 Listeners
The event manager EventManager will trigger the listener's development method through the configuration file when the event is triggered. In the 5.1.1 configuration file we configured the Demoevent listener as the App\listener\demolistener handle method, you can see the implementation of this class:
Phpnamespace App\listener; Use app\event\demoevent; Use Minor\event\listener; class extends listener{ publicfunction$event) { Echo $event->getname (). success!
'; }}
http://www.bkjia.com/PHPjc/1135179.html www.bkjia.com true http://www.bkjia.com/PHPjc/1135179.html techarticle Minor "PHP framework" 5. Events, minorphp Framework Event 5.1 event class Minor provides a simple observer implementation that allows you to subscribe and listen to events in your application. 5 .... .