[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 //!! Observable 3 //! An abstract class implementing observable objects 4 /*! 5 \abstract 6 Methods to override: NONE 7 This class implements the Observer design pattern defining Observable 8 objects, when a class extends Observable Observers can be attached to 9 the class listening for some event. When an event is detected in any 10 method of the derived class the method can call notifyAll($event,$msg) 11 to notify all the observers listening for event $event. 12 The Observer objects must extend the Observer class and define the 13 notify($event,$msg) method. 14 */ 15 class Observable { 16 var $_observers=Array(); 17 18 function Observable() { 19 20 } 21 22 /*! 23 This method can be used to attach an object to the class listening for 24 some specific event. The object will be notified when the specified 25 event is triggered by the derived class. 26 */ 27 function attach($event, &$obj) 28 { 29 if (!is_object($obj)) { 30 return false; 31 } 32 $obj->_observerId = uniqid(rand()); 33 $this->_observers[$event][$obj->_observerId] = &$obj; 34 } 35 36 /*! 37 Attaches an object to the class listening for any event. 38 The object will be notified when any event occurs in the derived class. 39 */ 40 function attach_all(&$obj) 41 { 42 if (!is_object($obj)) { 43 return false; 44 } 45 $obj->_observerId = uniqid(rand()); 46 $this->_observers['all'][$obj->_observerId] = &$obj; 47 } 48 49 /*! 50 Detaches an observer from the class. 51 */ 52 function dettach(&$obj) 53 { 54 if (isset($this->_observers[$obj->_observerId])) { 55 unset($this->_observers[$obj->_observerId]); 56 } 57 } 58 59 /*! 60 \protected 61 Method used to notify objects of an event. This is called in the 62 methods of the derived class that want to notify some event. 63 */ 64 function notify_all($event, $msg) 65 { 66 //reset($this->_observers[$event]); 67 if(isset($this->_observers[$event])) { 68 foreach ($this->_observers[$event] as $observer) { 69 $observer->notify($event,$msg); 70 } 71 } 72 if(isset($this->_observers['all'])) { 73 foreach ($this->_observers['all'] as $observer) { 74 $observer->notify($event,$msg); 75 } 76 } 77 78 } 79 80 } 81 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |