Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePoint PHP Blog:
An Introduction into Event Loops in PHP
Sep 10, 2015 @ 15:06:55

The SitePoint PHP blog has posted a tutorial from Christopher Pitt introducing you to using event loops in PHP, a feature that allows for asynchronous processing, executing code while waiting for other code to finish.

PHP developers are always waiting for something. Sometimes we’re waiting for requests to remote services. Sometimes we’re waiting for databases to return rows from a complex query. Wouldn’t it be great if we could do other things during all that waiting?

If you’ve written some JS, you’re probably familiar with callbacks and DOM events. And though we have callbacks in PHP, they don’t work in quite the same way. That’s thanks to a feature called the event loop.

He starts by explaining event loops with an example from a language that naturally supports it - Javascript. He includes another example using the setTimeout function in Javascript to show a simple loop but points out that PHP just doesn't support this same kind of handling (code included). So, how can you simulate the loop like in JS? He links to and includes a examples of two libraries that could be dropped in and used to do the hard work behind the scenes of the looping:

He suggests that PHP developer "get out of the single threaded mindset" and work in ways to handle asynchronous processing into their code to improve performance and flexibility.

tagged: event loop javascript library icicle reactphp asynchronous processing

Link: http://www.sitepoint.com/an-introduction-into-event-loops-in-php/


Trending Topics: