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

Symfony Finland:
Going Async in Symfony Controllers
Feb 22, 2016 @ 16:50:25

On the Symfony Finland site Jani Tarvainen has posted a tutorial showing you how to create asynchronous controller handling in a Symfony-based application.

Asynchronous programming has become a synonym for high performance in server side web applications in the recent years. This is largely due to the rising popularity of JavaScript and Node.js, in which everything is async by default. [...] So asynchronous programming does not push your computer into overdrive to enable higher performance. What it can do is help the computer to use it's resources more efficiently, by removing time spent waiting.

He then talks about PHP's typical flow model - synchronous and single-threaded. While it does make it simpler to debug/understand it also limits it and can cause higher processing times. Thanks to some other projects, however, asynchronous development with PHP has become more of a reality. He shows how to use one of these projects, Icicle, and its coroutines functionality to make a Symfony controller that handles calls to a sayHello method asynchronously, returning messages in a fraction of the normal processing time.

tagged: asynchronous controller tutorial icicle wait symfony

Link: https://www.symfony.fi/entry/going-async-in-symfony-controllers

SitePoint PHP Blog:
Build a Superfast PHP Server in Minutes with Icicle
Sep 17, 2015 @ 16:21:44

The SitePoint PHP blog has posted a new tutorial by Christopher Pitt showing you how to build a PHP server "super fast" with the help of the Icicle/http library and some event-driven programming techniques.

Event-based programming is a strange topic for PHP developers. In a language as procedural; events are little more than function calls. Nothing happens between events, and all meaningful code is still blocking.

Languages like JavaScript show us what PHP could be like if event loops were at the center. Some folks have taken these insights and coded them into event loops and HTTP servers. Today we’re going to create an HTTP server, in PHP. We’ll connect it to Apache to serve static files quickly. Everything else will pass through our PHP HTTP server, based on Icicle.

They start off showing you how to configure your Apache server to rewrite the requests (only for non-existent files) to the PHP handler. From there, he helps you get the Icicle/http library installed and create a simple HTTP server with it's included functionality. He shows how to set up routing using the LeagueRoute package and return correct HTTP response codes based on the result of the request. Finally he shows the use of the LeaguePlates library to render more complex views than just plain-text results.

tagged: tutorial http server icicle league plates route

Link: http://www.sitepoint.com/build-a-superfast-php-server-in-minutes-with-icicle/

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: