i am just playing around with php to create a plugin architecture. for this i am using the event listener ( mediator ) pattern where a code can listen/fire an event.
my real question is how do i make the listener object available through out the application.' THe only solutions i can think of right now are making the core listener / observer class singleteon so any code from anywhere can register a listener.
$listener = Plugin::getInstance();
$listener->addEventListener('savePost', ....);
and for firing also i can use the same process.
OR
making only the registered event array static for example
private static $registeredEvents = array();
public function addEventListener($eventName, Closure $c){
self::$registeredEvents[$eventName] = $c;
}
If i do this no matter how the plugin/listener object is provided to any client code the same container ( $registeredEvents ) will be used.
Otherwise providing new instances of the listener class to other objects results in having dispersed registered events. So when firing specific events from the system, i cannot loop through each and every registered events from different instances.
So plz anyone show me more efficient way of doing this.