-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrait_observer.php
More file actions
69 lines (50 loc) · 1.03 KB
/
Copy pathtrait_observer.php
File metadata and controls
69 lines (50 loc) · 1.03 KB
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
64
65
66
67
68
<?php
interface IListener {
public function notify();
}
class HogeListener implements IListener{
public function notify(){
echo "HogeHoge!!\n";
}
}
class HomuListener implements IListener{
public function notify(){
echo "HomuHomu!!\n";
}
}
trait Notifier {
protected $listeners = array();
protected function addListener(IListener $listener){
$this->listeners[] = $listener;
return $this;
}
protected function clearListener(){
$this->listeners = array();
return $this;
}
protected function notify(){
foreach($this->listeners as $listener){
$listener->notify();
}
}
}
class Something {
use Notifier;
public function __construct(){
$this
->addListener(new HogeListener())
->addListener(new HomuListener())
;
}
public function doSomething(){
echo "do!!\n";
$this->notify();
}
public function clear(){
$this->clearListener();
}
}
$something = new Something();
$something->doSomething();
$something->clear();
$something->doSomething();