You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/listeners-and-event-handlers.md
+24-24Lines changed: 24 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -6,40 +6,40 @@
6
6
There is no specific internal event handling mechanism, but one can be approximated by implementing one yourself. For example, a basic implementation of an [observer model](https://en.wikipedia.org/wiki/Observer_pattern) event handling system:
7
7
```php
8
8
<?php
9
-
abstract class SubjectBase {
10
-
abstract public function handleEvent();
9
+
abstract class SubjectBase {
10
+
abstract public function handleEvent();
11
11
}
12
12
13
-
class Observer {
14
-
private $subjects = array();
13
+
class Observer {
14
+
private $subjects = array();
15
15
public function registerSubject($subject) {
16
-
array_push($this->subjects, $subject);
16
+
array_push($this->subjects, $subject);
17
17
}
18
18
public function notifySubjects() {
19
19
foreach($this->subjects as $s) {
20
-
$s->handleEvent();
20
+
$s->handleEvent();
21
21
}
22
22
}
23
23
}
24
24
class Subject extends SubjectBase {
25
-
private $name;
25
+
private $name;
26
26
public function __construct($name) {
27
-
$this->name = $name;
27
+
$this->name = $name;
28
28
}
29
-
public function handleEvent($event) { // In this example, events are just Strings. However more a more sophisticated Event object could be constructed and utilized to increase usefulness
30
-
print "Subject name: ".$this->name." is handling this event: ".$event;
29
+
public function handleEvent($event) { // In this example, events are just Strings. However more a more sophisticated Event object could be constructed and utilized to increase usefulness
30
+
print "Subject name: ".$this->name." is handling this event: ".$event;
31
31
}
32
32
}
33
-
33
+
34
34
$a = new Subject("foo");
35
35
$b = new Subject("bar");
36
36
$c = new Subject("baz");
37
-
37
+
38
38
$observer = new Observer();
39
39
$observer->registerSubject($a);
40
40
$observer->registerSubject($b);
41
41
$observer->registerSubject($c);
42
-
42
+
43
43
$observer->notifySubjects("myEvent");
44
44
?>
45
45
```
@@ -53,19 +53,19 @@ Subject name: baz is handling this event: myEvent
53
53
Much like PHP, there exists no native library functions specifically for event handling. Yet, just like PHP, event-based architectures can be created. A basic observer model pattern can be approximated in a way similar to which follows:
54
54
```python
55
55
classSubject:
56
-
def__init__(self, name):
57
-
self.name = name
58
-
defhandleEvent(self, event):
59
-
print("Subject name: %s is handling this event: %s"%(self.name, event))
60
-
56
+
def__init__(self, name):
57
+
self.name = name
58
+
defhandleEvent(self, event):
59
+
print("Subject name: %s is handling this event: %s"%(self.name, event))
0 commit comments