Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?PHP
2
/**
3
 * example that show how to use objects as observers without
4
 * loosing references
5
 *
6
 * @package    Event_Dispatcher
7
 * @subpackage Examples
8
 * @author     Stephan Schmidt <schst@php.net>
9
 */
10
 
11
/**
12
 * load Event_Dispatcher package
13
 */
14
require_once 'Event/Dispatcher.php';
15
 
16
/**
17
 * example sender
18
 */
19
class sender
20
{
21
    var $_dispatcher = null;
22
 
23
    function sender(&$dispatcher)
24
    {
25
        $this->_dispatcher = &$dispatcher;
26
    }
27
 
28
    function foo()
29
    {
30
        $notification = &$this->_dispatcher->post($this, 'onFoo', 'Some Info...');
31
        echo "notification::foo is {$notification->foo}<br />";
32
    }
33
}
34
 
35
/**
36
 * example observer
37
 */
38
class receiver
39
{
40
    var $foo;
41
 
42
    function notify(&$notification)
43
    {
44
        echo "received notification<br />";
45
        echo "receiver::foo is {$this->foo}<br />";
46
        $notification->foo = 'bar';
47
    }
48
}
49
 
50
$dispatcher = &Event_Dispatcher::getInstance();
51
 
52
$sender = &new sender($dispatcher);
53
$receiver = new receiver();
54
$receiver->foo = 42;
55
 
56
// make sure you are using an ampersand here!
57
$dispatcher->addObserver(array(&$receiver, 'notify'));
58
 
59
$receiver->foo = 'bar';
60
 
61
echo 'sender->foo()<br />';
62
$sender->foo();
63
?>