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