Implement PHP events based on C # event ideas

Source: Internet
Author: User

Event definition

When we use the delegate scenario, we would like to have such two roles appearing: broadcasters and Subscribers. We need these two roles to implement a very common scenario of subscribing and broadcasting.

The broadcaster's role should have the ability to include a delegate field to emit a broadcast by invoking a delegate. Subscribers should have the ability to decide when to start or stop a subscription by calling + = and-=.

An event is a word that describes the pattern of the scene. Events are a subset of delegates that are born to meet the needs of the broadcast/subscription mode.

Simple implementation of events in C #

usingSystem;namespaceconsoleapplication2{classProgram {Static voidMain (string[] args) {ObServer ob=NewObServer (); Ob. Changed+=Change ; Ob. Trigger ("Hello Kitty");        Console.ReadLine (); }        Static voidChange (stringoutput)        {Console.WriteLine (output); }    }    classObServer {/// <summary>        ///Events/// </summary>         Public Eventaction<string>Changed; /// <summary>        ///Triggering Events/// </summary>        /// <param name= "Init" ></param>         Public voidTrigger (stringinit)        {Changed (init); }    }}

In fact, when executing the A function, the B-Function procedure is executed automatically, and the changing of the delegate is automatically triggered when the Init is executed, and the binding executes print.

Observer itself does not implement changed, leaving the business outside to handle the coupling of complex systems in order to facilitate it.

In combination with this idea, PHP implements similar events

<?PHPclassobservice{ Public $even=NULL;  Public  functionTrigger$init){        Call_user_func($this->even,$init); }}$ob=NewObservice ();$ob->even = "Chanage";$ob->trigger ("Hello kitty!");functionChanage ($str){    Echo($str);}?>

Summary: Check out some of the online PHP implementation events look quite complex, here is just a personal understanding of the implementation of a simple PHP event ideas, not necessarily a standard PHP event.

Implement PHP events based on C # event ideas

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.