expand description
[m6w6/hikke] / README.md
1 # hikke\Event
2
3 Prioritized event observers. [![Build Status](https://travis-ci.org/m6w6/hikke.svg)](https://travis-ci.org/m6w6/hikke)
4
5 Example:
6
7 ```php
8 <?php
9
10 use hikke\Event;
11
12 class Observer implements \SplObserver {
13 private $name;
14 function __construct($name) {
15 $this->name = $name;
16 }
17 function update(\SplSubject $e) {
18 echo "Observer '{$this->name}' notified by '$e' ({$e->getPriority()})\n";
19 }
20 function proxiedMethodCall($arg) {
21 $this->name .= $arg;
22 }
23 }
24
25 $event = new Event("my_event");
26 $event->attach(new Observer("o1"), 1);
27 $event->attach(new Observer("o2"), 2);
28 $event->notify();
29
30 ?>
31 ```
32
33 Output:
34
35 ```
36 Observer 'o1' notified by 'my_event' (0)
37 Observer 'o2' notified by 'my_event' (0)
38 ```
39
40 Another example:
41
42 ```php
43 <?php
44
45 $proxy = new Event\Proxy;
46 $proxy->ev1 = 0;
47 $proxy->ev2 = 1;
48 $proxy->attach(new Observer("o1"), null, 1);
49 $proxy->attach(new Observer("o2"), null, 0);
50 $proxy->attach(new Observer("o3"), "ev2");
51 $proxy->ev3->attach(new Observer("o2"));
52
53 $proxy->proxiedMethodCall("-proxy");
54 $proxy->notify();
55 ?>
56 ```
57
58 Output:
59
60 ```
61 Observer 'o2-proxy' notified by 'default' (0.001)
62 Observer 'o1-proxy' notified by 'default' (0.001)
63 Observer 'o2-proxy' notified by 'ev1' (0.002)
64 Observer 'o1-proxy' notified by 'ev1' (0.002)
65 Observer 'o2-proxy' notified by 'ev3' (0.004)
66 Observer 'o2-proxy' notified by 'ev2' (1.003)
67 Observer 'o3-proxy' notified by 'ev2' (1.003)
68 Observer 'o1-proxy' notified by 'ev2' (1.003)
69 ```