Call us: 0031 23-75-10500  |  Language: Dutch English German
 
The PxEventSubject Class

PxEventSubject: The object capable to fire events

If you want to be able to fire events from an object, you should implement the PxEventSubject interface. By
implementing this method it becomes possible for observers to attach themselves to the object and to be
notified when an event occurs.

Most of the methods have a very straightforward implementation. For that reason we could just as well have
created an abstract base class instead of an interface. However, because of the lack of multiple inheritance
in PHP, we have decided to use an interface, so that it still is possible to extend from some other base class
and implement the PxEventSubject class anyway. You can pass on all methods directly to an instance of the
PxEventObserverCollection class.

Do you need a straight-forward implementation of the PxEventSubject interface and does your class not have a
base class? In that situation you can also directly extend from the PxEventObserverCollection class.

Synopsis


Class PxEventSubject

Methods

public PxEventSubject PxEventSubject::attachObserver ( PxEventObserver $observer , [string $event = NULL] )
public mixed PxEventSubject::fireEvent ( PxEvent $event )

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
{

}