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!
'; }}
The above describes the Golden field a juvenile event book Hong Kong Kowloon Treasure Murder incident Minor5 incident, including the Golden field A juvenile event book Hong Kong Kowloon Treasure Murder Incident content, I hope to be interested in PHP tutorial friends helpful.