There must be an instance of class A before the event is triggered because you must register for that event. An exception would be if you'd register a static method. Show Let's say you have an User class which should trigger an event. First you need an (abstract) event dispatcher class. This kind of event system works like ActionScript3:
Your User class can now extend that Dispatcher:
And how to register for that event? Say you have class A with
method
If you have proper autoloading the static method can be called. In both cases the
The EventDispatcher Component
IntroductionObject-oriented code has gone a long way to ensuring code extensibility. By creating classes that have well-defined responsibilities, your code becomes more flexible and a developer can extend them with subclasses to modify their behaviors. But if they want to share the changes with other developers who have also made their own subclasses, code inheritance is no longer the answer. Consider the real-world example where you want to provide a plugin system for your project. A plugin should be able to add methods, or do something before or after a method is executed, without interfering with other plugins. This is not an easy problem to solve with single inheritance, and even if multiple inheritance was possible with PHP, it comes with its own drawbacks. The Symfony EventDispatcher component implements the Mediator and Observer design patterns to make all these things possible and to make your projects truly extensible. Take an example from
the HttpKernel component. Once a
InstallationNote If you install this
component outside of a Symfony application, you must require the UsageSee also This article explains how to use the EventDispatcher features as an independent component in any PHP application. Read the Events and Event Listeners article to learn about how to use it in Symfony applications. EventsWhen an event is dispatched, it's identified by a unique name (e.g. Naming ConventionsThe unique event name can be any string, but optionally follows a few naming conventions:
Event Names and Event ObjectsWhen the dispatcher notifies listeners, it passes an actual Often times, data about a specific event needs to be passed along with the The DispatcherThe dispatcher is the central object of the event dispatcher system. In general, a single dispatcher is created, which maintains a registry of listeners. When an event is dispatched via the dispatcher, it notifies all listeners registered with that event: Connecting ListenersTo take advantage of an existing event, you need to connect a listener to the dispatcher so that it can be notified when the event is dispatched. A call to the dispatcher's The
Note A PHP callable is a PHP variable that can be used by the So far, you've seen how PHP objects can be registered as listeners. You can also register PHP Closures as event listeners: Once a listener is registered with the dispatcher, it waits until the event is notified. In the above example, when the The Creating and Dispatching an EventIn addition to registering listeners with existing events, you can create and dispatch your own events. This is useful when creating third-party libraries and also when you want to keep different components of your own system flexible and decoupled. Creating an Event ClassSuppose you want to create a new event - Each listener
now has access to the order via the Note If you don't need to pass any additional data to the event listeners, you can also use the default
Event class. In such case, you can document the event and its name in a generic Dispatch the EventThe
dispatch() method notifies all listeners of the given event. It takes two arguments: the Notice that the special Using Event SubscribersThe most common way to listen to an event is to register an event listener with the dispatcher. This listener can listen to one or more events and is notified each time those events are dispatched. Another way to listen to events is via an event subscriber. An event subscriber is a PHP class that's able to tell the dispatcher exactly which events it should subscribe to. It implements the
EventSubscriberInterface interface, which requires a single static method called
getSubscribedEvents(). Take the following example of a subscriber that subscribes to the This is very similar to a listener class, except that the class itself can tell the dispatcher which events it should listen to. To register a subscriber with the dispatcher, use the addSubscriber() method: The dispatcher will automatically register the subscriber for each event returned by the The example above shows how to register several listener methods for the same event in subscriber and also shows how to pass the priority of each listener method. The higher the number, the earlier the method is called. In the above example, when the Stopping Event Flow/PropagationIn some cases, it may make sense for a listener to prevent any other listeners from being called. In other words, the listener needs to be able to tell the dispatcher to stop all propagation of the event to future listeners (i.e. to not notify any more listeners). This can be accomplished from inside a listener via the stopPropagation() method: Now, any listeners to It is possible to detect if an event was stopped by using the isPropagationStopped() method which returns a boolean value: EventDispatcher Aware Events and ListenersThe Event Name IntrospectionThe Learn More
|