@@ -50,7 +50,7 @@ $em = new EventManager();
50
50
$myEvent = new Event('name', [ 'some params ...' ]);
51
51
```
52
52
53
- - 使用基础了 ` Event ` 的子类
53
+ - 使用继承了 ` Event ` 的子类
54
54
55
55
这样你可以追加自定义数据
56
56
@@ -94,9 +94,6 @@ class ExamListener1
94
94
echo "handle the event {$event->getName()}\n";
95
95
}
96
96
}
97
-
98
- // add
99
- $dispatcher->attach(Mailer::EVENT_MESSAGE_SENT, new MyListener);
100
97
```
101
98
102
99
- 一个类(含有 ` __invoke ` 方法)
@@ -109,9 +106,6 @@ class ExamListener2
109
106
echo "handle the event {$event->getName()}\n";
110
107
}
111
108
}
112
-
113
- // add
114
- $dispatcher->attach(Mailer::EVENT_MESSAGE_SENT, new MyListener);
115
109
```
116
110
117
111
- 一个类(implements the HandlerInterface)
@@ -128,12 +122,9 @@ class ExamHandler implements HandlerInterface
128
122
// TODO: Implement handle() method.
129
123
}
130
124
}
131
-
132
- // add
133
- $dispatcher->attach(Mailer::EVENT_MESSAGE_SENT, new MyListener);
134
125
```
135
126
136
- ### 触发事件
127
+ ### 绑定事件触发
137
128
138
129
``` php
139
130
// use trigger
@@ -156,23 +147,26 @@ class Mailer
156
147
}
157
148
```
158
149
159
- - 触发
150
+ ### 触发事件
160
151
161
152
``` php
162
153
$em = new EventManager();
163
- $em->attach('messageSent', 'exam_handler');
164
- $em->attach('messageSent', function (EventInterface $event)
154
+
155
+ // 绑定事件
156
+ $em->attach(Mailer::EVENT_MESSAGE_SENT, 'exam_handler');
157
+ $em->attach(Mailer::EVENT_MESSAGE_SENT, function (EventInterface $event)
165
158
{
166
159
$pos = __METHOD__;
167
160
echo "handle the event {$event->getName()} on the: $pos\n";
168
161
});
169
- $em->attach('messageSent' , new ExamListener1());
170
- $em->attach('messageSent' , new ExamListener2());
171
- $em->attach('messageSent' , new ExamHandler());
162
+ $em->attach(Mailer::EVENT_MESSAGE_SENT , new ExamListener1());
163
+ $em->attach(Mailer::EVENT_MESSAGE_SENT , new ExamListener2());
164
+ $em->attach(Mailer::EVENT_MESSAGE_SENT , new ExamHandler());
172
165
173
166
$mailer = new Mailer();
174
167
$mailer->setEventManager($em);
175
168
169
+ // 执行,触发事件
176
170
$mailer->send('hello, world!');
177
171
```
178
172
0 commit comments