The PxEventObserverCollection Class
PxEventObserverCollection: Collection of observer objects
When you implement that PxEventSubject interface, you need to implement the attachObserver(), detachObserver()
and fireEvent() methods. You can either do this from scratch, and create your own list of attached observers,
or you can use (which is our advise) this PxEventObserver class for it. This class already contains
implementations for all those methods and all you need to do is pass on the method calls to an instance of
this class.
If you are implementing the PxEventSubject interface, while the class you are implementing it on does have a
base class, you could event extend from this PxEventObserver class, so that you do not even have to implement
the methods from the interface.
So, you can ask yourself the following questions:
1. Do you want instances of your class to be able to fire events? No: Why are you then even using this
library? Go away! Yes: Continue to 2.
2. Does your class have a base class that it extends? No: Extend from PxEventObserverCollection. That's
all. Yes: Continue to 3.
3. Do the following: add a member PxEventObserverCollection member variable to your class, and implement all
methods from the PxEventSubject interface by calling the method with the same name in the observer-collection.
and fireEvent() methods. You can either do this from scratch, and create your own list of attached observers,
or you can use (which is our advise) this PxEventObserver class for it. This class already contains
implementations for all those methods and all you need to do is pass on the method calls to an instance of
this class.
If you are implementing the PxEventSubject interface, while the class you are implementing it on does have a
base class, you could event extend from this PxEventObserver class, so that you do not even have to implement
the methods from the interface.
So, you can ask yourself the following questions:
1. Do you want instances of your class to be able to fire events? No: Why are you then even using this
library? Go away! Yes: Continue to 2.
2. Does your class have a base class that it extends? No: Extend from PxEventObserverCollection. That's
all. Yes: Continue to 3.
3. Do the following: add a member PxEventObserverCollection member variable to your class, and implement all
methods from the PxEventSubject interface by calling the method with the same name in the observer-collection.
Synopsis
Class PxEventObserverCollection implements PxEventSubject
Methods
public PxEventSubject PxEventObserverCollection::attachObserver ( PxEventObserver $observer , [string $event = NULL] )
Examples
Example
The following code shows a simple implementation of the PxEventSubject
interface, where you make use of the PxEventObserver class to hold all
registered observers.
class SomeClass extends SomeBaseClass implements PxEventSubject
{
// all observers
private $observers;
// constructor
public function __construct()
{
$this->observers = new PxEventObserverCollection();
}
// add an observer
public function attachObserver(PxEventObserver $observer, $events = null)
{
$this->observers->attachObserver($observer, $events);
return $this;
}
// remove an observer
public function detachObserver(PxEventObserver $observer, $events = null)
{
$this->observers->detachObserver($observer, $events);
return $this;
}
// fire an event
public function fireEvent(PxEvent $event)
{
$this->observers->fireEvent($event);
return $this;
}
If your class does not have a base class, you can have the same result
by just extending from PxEventObserverCollection:
class SomeClass extends PxEventObserverCollection
{
}
