Php observer mode example

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.