-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.php
More file actions
executable file
·46 lines (36 loc) · 950 Bytes
/
observer.php
File metadata and controls
executable file
·46 lines (36 loc) · 950 Bytes
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
<?php
/*
one-to-many dependency between objects
when one object changes, all dependents are notified (and updated) automatically
good examples: database triggers, dom changes, screen updates
*/
// recipients
// message send (notification)
// system (notifier)
class Publisher {
private $recipients = Array();
public function subscribeIt(Observer $recipient) {
$this->recipients[] = $recipient;
}
public function unsubscribeIt() {}
public function notify() {
// loop through subscribers, and message them
foreach ($this->recipients as $recipient) {
$recipient->receiveMessage($this);
}
}
}
class Observer {
// data vars
public function __construct() {
// basic stuff
}
public function receiveMessage(Publisher $message) {
var_dump($message);
}
}
$system = new Publisher();
$system->subscribeIt(new Observer);
$system->subscribeIt(new Observer);
$system->notify();
?>