1 /**2 * Event Generation class3 * Class Eventgenerator4 */5 Abstract classEventgenerator6 {7 Private $ObServers= [];8 9 //Increase the viewerTen Public functionAdd (ObServer$ObServer) One { A $this->observers[] =$ObServer; - } - the //Event Notification - Public functionNotify () - { - foreach($this->observers as $ObServer) { + $ObServer-update (); - } + } A at } - - /** - * Viewer Interface class - * Interface ObServer - */ in InterfaceObServer - { to Public functionUpdate$event _info=NULL); + } - the /** * * Viewer 1 $ */Panax Notoginseng classObServer1ImplementsObServer - { the Public functionUpdate$event _info=NULL) + { A Echo"Observer 1 received execution notification execution complete!" \ n "; the } + } - $ /** $ * Viewer 2 - */ - classObServer2ImplementsObServer the { - Public functionUpdate$event _info=NULL)Wuyi { the Echo"Observer 2 received execution notification execution complete!" \ n "; - } Wu } - About /** $ * Event - * Class Event - */ - classEventextendsEventgenerator A { + /** the * Trigger Event - */ $ Public functionTrigger () the { the //notify the viewer the $this-notify (); the } - } in the //Create an event the $event=NewEvent (); About //adding bystanders to events the $event->add (NewObServer1 ()); the $event->add (NewObServer2 ()); the //Execute event Notification bystanders + $event->trigger ();
The observation mode of PHP design mode