Dispatch notifications using PHP callbacks
The Event_Dispatcher acts acts as a notification dispatch table. It is used to notify other objects of interesting things, if they meet certain criteria. This information is encapsulated in Event_Notification objects. Client objects register themselves with the Event_Dispatcher as observers of specific notifications posted by other objects. When an event occurs, an object posts an appropriate notification to the Event_Dispatcher. The Event_Dispatcher dispatches a message to each registered observer, passing the notification as the sole argument.
The Event_Dispatcher is actually a combination of three design patterns: the Singleton, Mediator, and Observer patterns. The idea behind Event_Dispatcher is borrowed from Apple's Cocoa framework.
This method registers a callback which is called when the notification corresponding to the criteria given at registration time is posted. The criteria are the notification name and eventually the class of the object posted with the notification.
If there are any pending notifications corresponding to the criteria given here, the callback will be called straight away.
If the notification name is empty, the observer will receive all the posted notifications. Same goes for the class name.
A PHP callback
Expected notification name, serves as a filter
Expected contained object class, serves as a filter
The purpose of the optional associated object is generally to pass the object posting the notification to the observers, so that the observers can query the posting object for more information about the event.
Notifications are by default added to a pending notification list. This way, if an observer is not registered by the time they are posted, it will still be notified when it is added as an observer. This behaviour can be turned off in order to make sure that only the registered observers will be notified.
The info array serves as a container for any kind of useful information. It is added to the notification object and posted along.