@@ -15,6 +15,7 @@ single [`run()`](#run) call that is controlled by the user.
15
15
* [ Usage] ( #usage )
16
16
* [ Loop] ( #loop )
17
17
* [ Loop methods] ( #loop-methods )
18
+ * [ Loop autorun] ( #loop-autorun )
18
19
* [ get()] ( #get )
19
20
* [ ~~ Factory~~ ] ( #factory )
20
21
* [ ~~ create()~~ ] ( #create )
@@ -76,8 +77,6 @@ Loop::addPeriodicTimer(5, function () {
76
77
$formatted = number_format($memory, 3).'K';
77
78
echo "Current memory usage: {$formatted}\n";
78
79
});
79
-
80
- Loop::run();
81
80
```
82
81
83
82
See also the [ examples] ( examples ) .
@@ -98,8 +97,6 @@ Loop::addTimer(1.0, function () use ($timer) {
98
97
Loop::cancelTimer($timer);
99
98
echo 'Done' . PHP_EOL;
100
99
});
101
-
102
- Loop::run();
103
100
```
104
101
105
102
As an alternative, you can also explicitly create an event loop instance at the
@@ -127,12 +124,13 @@ In both cases, the program would perform the exact same steps.
127
124
1 . The event loop instance is created at the beginning of the program. This is
128
125
implicitly done the first time you call the [ ` Loop ` class] ( #loop ) or
129
126
explicitly when using the deprecated [ ` Factory::create() method ` ] ( #create )
130
- (or manually instantiating any of the [ loop implementation ] ( #loop-implementations ) ).
127
+ (or manually instantiating any of the [ loop implementations ] ( #loop-implementations ) ).
131
128
2 . The event loop is used directly or passed as an instance to library and
132
129
application code. In this example, a periodic timer is registered with the
133
130
event loop which simply outputs ` Tick ` every fraction of a second until another
134
131
timer stops the periodic timer after a second.
135
- 3 . The event loop is run at the end of the program with a single [ ` run() ` ] ( #run )
132
+ 3 . The event loop is run at the end of the program. This is automatically done
133
+ when using [ ` Loop ` class] ( #loop ) or explicitly with a single [ ` run() ` ] ( #run )
136
134
call at the end of the program.
137
135
138
136
As of ` v1.2.0 ` , we highly recommend using the [ ` Loop ` class] ( #loop ) .
@@ -176,8 +174,6 @@ Loop::addTimer(1.0, function () use ($timer) {
176
174
Loop::cancelTimer($timer);
177
175
echo 'Done' . PHP_EOL;
178
176
});
179
-
180
- Loop::run();
181
177
```
182
178
183
179
On the other hand, if you're familiar with object-oriented programming (OOP) and
@@ -208,14 +204,31 @@ class Greeter
208
204
$greeter = new Greeter(Loop::get());
209
205
$greeter->greet('Alice');
210
206
$greeter->greet('Bob');
211
-
212
- Loop::run();
213
207
```
214
208
215
209
Each static method call will be forwarded as-is to the underlying event loop
216
210
instance by using the [ ` Loop::get() ` ] ( #get ) call internally.
217
211
See [ ` LoopInterface ` ] ( #loopinterface ) for more details about available methods.
218
212
213
+ #### Loop autorun
214
+
215
+ When using the ` Loop ` class, it will automatically execute the loop at the end of
216
+ the program. This means the following example will schedule a timer and will
217
+ automatically execute the program until the timer event fires:
218
+
219
+ ``` php
220
+ use React\EventLoop\Loop;
221
+
222
+ Loop::addTimer(1.0, function () {
223
+ echo 'Hello' . PHP_EOL;
224
+ });
225
+ ```
226
+
227
+ As of ` v1.2.0 ` , we highly recommend using the ` Loop ` class this way and omitting any
228
+ explicit [ ` run() ` ] ( #run ) calls. For BC reasons, the explicit [ ` run() ` ] ( #run )
229
+ method is still valid and may still be useful in some applications, especially
230
+ for a transition period towards the more concise style.
231
+
219
232
#### get()
220
233
221
234
The ` get(): LoopInterface ` method can be used to
@@ -262,8 +275,6 @@ class Greeter
262
275
$greeter = new Greeter(Loop::get());
263
276
$greeter->greet('Alice');
264
277
$greeter->greet('Bob');
265
-
266
- Loop::run();
267
278
```
268
279
269
280
See [ ` LoopInterface ` ] ( #loopinterface ) for more details about available methods.
0 commit comments