Re: Subject/Observer design pattern in PEAR

From: Date: Wed, 21 Jul 2004 20:53:15 +0000
Subject: Re: Subject/Observer design pattern in PEAR
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-32221@lists.php.net to get a copy of this message
Klaus Guenther wrote:
Philippe Jausions wrote:
Hi, Just wondering if the PEAR group advised about the member and method names to be used for the Subject/Observer design pattern? Apparently there is no consistency in the PEAR packages that use that pattern.
So far, there is no standard naming convention for such things. There was merely the private/public distinction. However, certain names have become established for certain functions (e.g., toHtml, toString, etc.). There certainly would be some benefit in promoting consistency :-) Klaus
Just to get the ball rolling then, not that it would necessarily lead to something, but wondering what would be the most flexible: <?php class MySubjectObserverClass { var $_listeners = array(); var $_listernerNextID = 0; // Alternative names: register, subscribe... function addListerner(&$obj) {
       $this->_listerners[] =& $obj;
      return $this->_listernerNextID++;
} // Alternative name: unregister, unsubscribe... function removeListerner($id) {
        unset($this->_listerners[$id]);
} // Alternative name: broadcast... function notifyAll($event) {
       foreach ($this->_listerners as $id => $obj)
       {
           $this->_listerners[$id]->notify($event, $this);
       }
} // Alternative name: listen... function notify($event, &$obj) {
        switch (get_class($obj)) {
            case 'blah':
               ...
       }
} } ?> -Philippe

« previous php.pear.dev (#32221) next »