Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 69 lines (60 sloc) 2.53 kB
1a6a07f @davglass Adding project
authored
1 <?php
2 class CustomEvent {
3 private $subscribers = Array();
4 private $name;
5 private $scope;
6
7 public function __construct($name, $scope=false) {
8 $this->name = $name;
9 $this->scope = $scope;
10 echo('<em>New Event Created ('.$name.') in scope ('.get_class($this->scope).')</em><br>');
11 }
12
13 public function subscribe($func, $scope='') {
14 $scope = ((is_object($scope)) ? $scope : $this->scope);
15 echo('$scope: '.get_class($scope).'<br>');
16 $this->subscribers[$func] = array('function' => $func, 'scope' => $scope);
17 echo('<em>Added Event to: '.$this->name.' ('.$func.')</em><br>');
18 }
19
20 public function fire() {
21 $passArgs = array($this->scope);
22 $numArgs = func_num_args();
23 echo('Number of arguments: '.$numArgs.'<br>');
24 if ($numArgs) {
25 $args = func_get_args();
26 $passArgs = array_merge($passArgs, $args);
27 for ($i = 0; $i <= $numArgs; $i++) {
28 echo "Argument $i is: " . $passArgs[$i] . "<br />\n";
29 }
30 }
31
32 reset($this->subscribers);
33 //echo('<pre>'.print_r($this, 1).'</pre>');
34 foreach ($this->subscribers as $event) {
35 echo('<em>Checking Fire Event: '.$event['function'].' in scope ('.get_class($event['scope']).')</em><br>');
36 if (is_object($event['scope'])) {
37 if (is_callable(array($event['scope'], $event['function']))) {
38 echo('<em>Fire Event: '.$event['function'].' in scope ('.get_class($event['scope']).')</em><br>');
39 call_user_func_array(array($event['scope'], $event['function']), $passArgs);
40 }
41 } else {
42 if (is_callable($event['function'])) {
43 echo('<em>Fire Event: '.$event['function'].'</em><br>');
44 call_user_func_array($event['function'], $passArgs);
45 }
46 }
47 }
48 }
49
50 public function unsubscribe($func) {
51 echo('<em>Event Removed: '.$func.'</em><br>');
52 unset($this->subscribers[$func]);
53 }
54
55 public function unsubscribeAll() {
56 reset($this->subscribers);
57 foreach ($this->subscribers as $event) {
58 echo('<em>Event Removed: '.$event['function'].'</em><br>');
59 unset($this->subscribers[$event['function']]);
60 }
61 }
62
63 public function __toString() {
64 return 'Custom Event ['.$this->name.'] (subscribers: '.count($this->subscribers).')';
65 }
66 }
67
68 ?>
Something went wrong with that request. Please try again.