C:\xampp\php\pear\examples\object.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?PHP
/**
 * example that show how to use objects as observers without
 * loosing references
 *
 * @package    Event_Dispatcher
 * @subpackage Examples
 * @author     Stephan Schmidt <schst@php.net>
 */

/**
 * load Event_Dispatcher package
 */
require_once 'Event/Dispatcher.php';

/**
 * example sender
 */
class sender
{
    var 
$_dispatcher null;
    
    function 
sender(&$dispatcher)
    {
        
$this->_dispatcher = &$dispatcher;
    }
    
    function 
foo()
    {
        
$notification = &$this->_dispatcher->post($this'onFoo''Some Info...');
        echo 
"notification::foo is {$notification->foo}<br />";
    }
}

/**
 * example observer
 */
class receiver
{
    var 
$foo;
    
    function 
notify(&$notification)
    {
        echo 
"received notification<br />";
        echo 
"receiver::foo is {$this->foo}<br />";
        
$notification->foo 'bar';
    }
}

$dispatcher = &Event_Dispatcher::getInstance();

$sender = &new sender($dispatcher);
$receiver = new receiver();
$receiver->foo 42;

// make sure you are using an ampersand here!
$dispatcher->addObserver(array(&$receiver'notify'));

$receiver->foo 'bar';

echo 
'sender->foo()<br />';
$sender->foo();
?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586