This article mainly introduces the application scenarios of the php observer mode, and analyzes in detail the definition and usage skills of the php observer mode based on the complete instance form, for more information, see the next article. This article mainly introduces the php Observer Mode application scenario, and analyzes the definition and usage skills of the php observer mode in combination with the complete instance form. For more information, see
This article describes the application of the php observer mode. We will share this with you for your reference. The details are as follows:
_ Observers as $ obs) $ obs-> onBuyTicketOver ($ this, $ ticket); // $ this can be used to obtain the topic class handle, use in notification} // add public function addObserver ($ observer) // add N notifications {$ this-> _ observers [] = $ observer ;}} #=========================================define multiple notifications ==============/// SMS log notification class HipiaoMSM implements ITicketObserver {public function onBuyTicketOver ($ sender, $ ticket) {echo (date ('Y-m-d H: I: s '). "SMS log record: ticket purchased successfully: $ ticket
") ;}} // Text log notification class HipiaoTxt implements ITicketObserver {public function onBuyTicketOver ($ sender, $ ticket) {echo (date ('Y-m-d H: I: s '). "Text log record: ticket purchased successfully: $ ticket
") ;}} // The deduction volume will be given a notification class HipiaoDiKou implements ITicketObserver {public function onBuyTicketOver ($ sender, $ ticket) {echo (date ('Y-m-d H: i: s '). "complimentary coupons: tickets are successfully purchased: $ ticket will receive one 10 yuan voucher.
");}} #=================================================================== =============$ Buy = new HipiaoBuy (); $ buy-> addObserver (new HipiaoMSM (); // add various notifications according to different business logic $ buy-> addObserver (new HipiaoTxt ()); $ buy-> addObserver (new HipiaoDiKou (); // purchase tickets $ buy-> buyTicket ("No. 1 in a row");?>
The running result is as follows:
10:25:45 SMS log record: ticket purchase successful: line 1 10:25:45 text log record: ticket purchase successful: line 1 10:25:45 complimentary coupon: ticket purchase successful: one row in the first row will be given a 10 yuan discount volume.