Skip to content

Commit 104087a

Browse files
committed
up readme
1 parent a2affae commit 104087a

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

README.md

+11-17
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ $em = new EventManager();
5050
$myEvent = new Event('name', [ 'some params ...' ]);
5151
```
5252

53-
- 使用基础了 `Event` 的子类
53+
- 使用继承了 `Event` 的子类
5454

5555
这样你可以追加自定义数据
5656

@@ -94,9 +94,6 @@ class ExamListener1
9494
echo "handle the event {$event->getName()}\n";
9595
}
9696
}
97-
98-
// add
99-
$dispatcher->attach(Mailer::EVENT_MESSAGE_SENT, new MyListener);
10097
```
10198

10299
- 一个类(含有 `__invoke` 方法)
@@ -109,9 +106,6 @@ class ExamListener2
109106
echo "handle the event {$event->getName()}\n";
110107
}
111108
}
112-
113-
// add
114-
$dispatcher->attach(Mailer::EVENT_MESSAGE_SENT, new MyListener);
115109
```
116110

117111
- 一个类(implements the HandlerInterface)
@@ -128,12 +122,9 @@ class ExamHandler implements HandlerInterface
128122
// TODO: Implement handle() method.
129123
}
130124
}
131-
132-
// add
133-
$dispatcher->attach(Mailer::EVENT_MESSAGE_SENT, new MyListener);
134125
```
135126

136-
### 触发事件
127+
### 绑定事件触发
137128

138129
```php
139130
// use trigger
@@ -156,23 +147,26 @@ class Mailer
156147
}
157148
```
158149

159-
- 触发
150+
### 触发事件
160151

161152
```php
162153
$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)
165158
{
166159
$pos = __METHOD__;
167160
echo "handle the event {$event->getName()} on the: $pos\n";
168161
});
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());
172165

173166
$mailer = new Mailer();
174167
$mailer->setEventManager($em);
175168

169+
// 执行,触发事件
176170
$mailer->send('hello, world!');
177171
```
178172

examples/exam.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ public function send($message)
6464
}
6565

6666
$em = new EventManager();
67-
$em->attach('messageSent', 'exam_handler');
68-
$em->attach('messageSent', function (EventInterface $event)
67+
$em->attach(Mailer::EVENT_MESSAGE_SENT, 'exam_handler');
68+
$em->attach(Mailer::EVENT_MESSAGE_SENT, function (EventInterface $event)
6969
{
7070
$pos = __METHOD__;
7171
echo "handle the event {$event->getName()} on the: $pos\n";
7272
});
73-
$em->attach('messageSent', new ExamListener1());
74-
$em->attach('messageSent', new ExamListener2());
75-
$em->attach('messageSent', new ExamHandler());
73+
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener1());
74+
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener2());
75+
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamHandler());
7676

7777
$mailer = new Mailer();
7878
$mailer->setEventManager($em);

0 commit comments

Comments
 (0)