@@ -15,6 +15,7 @@ single [`run()`](#run) call that is controlled by the user.
1515* [ Usage] ( #usage )
1616 * [ Loop] ( #loop )
1717 * [ Loop methods] ( #loop-methods )
18+ * [ Loop autorun] ( #loop-autorun )
1819 * [ get()] ( #get )
1920 * [ ~~ Factory~~ ] ( #factory )
2021 * [ ~~ create()~~ ] ( #create )
@@ -76,8 +77,6 @@ Loop::addPeriodicTimer(5, function () {
7677 $formatted = number_format($memory, 3).'K';
7778 echo "Current memory usage: {$formatted}\n";
7879});
79-
80- Loop::run();
8180```
8281
8382See also the [ examples] ( examples ) .
@@ -98,8 +97,6 @@ Loop::addTimer(1.0, function () use ($timer) {
9897 Loop::cancelTimer($timer);
9998 echo 'Done' . PHP_EOL;
10099});
101-
102- Loop::run();
103100```
104101
105102As 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.
1271241 . The event loop instance is created at the beginning of the program. This is
128125 implicitly done the first time you call the [ ` Loop ` class] ( #loop ) or
129126 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 ) ).
1311282 . The event loop is used directly or passed as an instance to library and
132129 application code. In this example, a periodic timer is registered with the
133130 event loop which simply outputs ` Tick ` every fraction of a second until another
134131 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 )
136134 call at the end of the program.
137135
138136As of ` v1.2.0 ` , we highly recommend using the [ ` Loop ` class] ( #loop ) .
@@ -176,8 +174,6 @@ Loop::addTimer(1.0, function () use ($timer) {
176174 Loop::cancelTimer($timer);
177175 echo 'Done' . PHP_EOL;
178176});
179-
180- Loop::run();
181177```
182178
183179On the other hand, if you're familiar with object-oriented programming (OOP) and
@@ -208,14 +204,31 @@ class Greeter
208204$greeter = new Greeter(Loop::get());
209205$greeter->greet('Alice');
210206$greeter->greet('Bob');
211-
212- Loop::run();
213207```
214208
215209Each static method call will be forwarded as-is to the underlying event loop
216210instance by using the [ ` Loop::get() ` ] ( #get ) call internally.
217211See [ ` LoopInterface ` ] ( #loopinterface ) for more details about available methods.
218212
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+
219232#### get()
220233
221234The ` get(): LoopInterface ` method can be used to
@@ -262,8 +275,6 @@ class Greeter
262275$greeter = new Greeter(Loop::get());
263276$greeter->greet('Alice');
264277$greeter->greet('Bob');
265-
266- Loop::run();
267278```
268279
269280See [ ` LoopInterface ` ] ( #loopinterface ) for more details about available methods.
0 commit comments