@@ -13,6 +13,8 @@ single [`run()`](#run) call that is controlled by the user.
1313
1414* [ Quickstart example] ( #quickstart-example )
1515* [ Usage] ( #usage )
16+ * [ Loop] ( #loop )
17+ * [ get()] ( #get )
1618 * [ Factory] ( #factory )
1719 * [ create()] ( #create )
1820 * [ Loop implementations] ( #loop-implementations )
@@ -45,32 +47,32 @@ single [`run()`](#run) call that is controlled by the user.
4547Here is an async HTTP server built with just the event loop.
4648
4749``` php
48- $loop = React\EventLoop\Factory::create() ;
50+ use React\EventLoop\Loop ;
4951
5052$server = stream_socket_server('tcp://127.0.0.1:8080');
5153stream_set_blocking($server, false);
5254
53- $loop ->addReadStream($server, function ($server) use ($loop ) {
55+ Loop::get() ->addReadStream($server, function ($server) {
5456 $conn = stream_socket_accept($server);
5557 $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
56- $loop ->addWriteStream($conn, function ($conn) use (& $data, $loop ) {
58+ Loop::get() ->addWriteStream($conn, function ($conn) use (& $data) {
5759 $written = fwrite($conn, $data);
5860 if ($written === strlen($data)) {
5961 fclose($conn);
60- $loop ->removeWriteStream($conn);
62+ Loop::get() ->removeWriteStream($conn);
6163 } else {
6264 $data = substr($data, $written);
6365 }
6466 });
6567});
6668
67- $loop ->addPeriodicTimer(5, function () {
69+ Loop::get() ->addPeriodicTimer(5, function () {
6870 $memory = memory_get_usage() / 1024;
6971 $formatted = number_format($memory, 3).'K';
7072 echo "Current memory usage: {$formatted}\n";
7173});
7274
73- $loop ->run();
75+ Loop::get() ->run();
7476```
7577
7678See also the [ examples] ( examples ) .
@@ -110,6 +112,28 @@ $loop->run();
110112 purposes.
1111133 . The loop is run with a single [ ` $loop->run() ` ] ( #run ) call at the end of the program.
112114
115+ ### Loop
116+
117+ The ` Loop ` class exists as a convenient global accessor for the event loop.
118+
119+ #### get()
120+
121+ The ` get(): LoopInterface ` method is the preferred way to get and use the event loop. With
122+ it there is no need to always pass the loop around anymore.
123+
124+ ``` php
125+ use React\EventLoop\Loop;
126+
127+ Loop::get()->addTimer(0.02, function () {
128+ echo 'World!';
129+ });
130+ Loop::get()->addTimer(0.01, function () {
131+ echo 'Hello ';
132+ });
133+
134+ Loop::get()->run();
135+ ```
136+
113137### Factory
114138
115139The ` Factory ` class exists as a convenient way to pick the best available
0 commit comments