title: Stateless & asynchronous web-server with PHP (and Symfony)
body: <p>TL;DR: I'm not sure this topic has its place on StackOverflow, but basically it's just a topic of debate and thinking about making PHP apps like we would do with NodeJS for example (stateless request flow, asynchronous calls, etc.)</p>
  
  <h2>The situation</h2>
  
  <p>We know NodeJS can be used as both a web-server and web-app.</p>
  
  <p>But for PHP, <a href="http://php.net/manual/features.commandline.webserver.php" rel="nofollow noreferrer">the internal web-server is not recommended for production</a> (so says the documentation).</p>
  
  <p>But, as Symfony full-stack is based on the <code>Kernel</code> which handles <code>Request</code> objects, it means we <em>should</em> be able  to send lots of requests to the same kernel, only if we could "bootstrap" the php web-server (not the app) by creating a kernel before listening to HTTP requests. And our router would only create a Request object and make the kernel handle it.</p>
  
  <p>But for this, a Symfony app has to be <strong>stateless</strong>, for example we need Doctrine to effectively clear its unit of work after a request, or maybe we would need to sort of <em>isolate</em> some components based on a request (By identifying a request with its unique PHP class reference id? Or by using other php processes?), and obviously, we would need more asynchronous things in PHP or in the way we use the internal web-server.</p>
  
  <h2>The main questions I sometimes ask myself, and now ask to the community</h2>
  
  <p>To clarify this, I have some questions about PHP:</p>
  
  <ol>
  <li>Why <em>exactly</em> is the internal PHP webserver not recommended for production?<br>
  I mean, if we can configure how the server is run and its "router" file, we should be able to use it like any PHP server, yes or no?</li>
  <li>How does it behaves internally? Is memory shared between two requests?<br>
  By using the router, it seems obvious to me that variables are not shared, else we could make nodejs-like apps, but it seems PHP is not capable of doing something like this.</li>
  <li>Is it really possible to make a full-stateless application with Symfony?<br>
  e.g. I send two different requests to the same kernel object, in this case, is there any possibility that the two requests create a conflict in Symfony core components?</li>
  <li>Actually, the idea of <em>"Create a kernel -> start server -> on request, make the kernel handle it"</em> behavior would be awesome, because it would be something quite similar to NodeJS, but actually, the PHP paradigm is not compatible with this because we would need <em>each request</em> to be handled asynchronously. But if a kernel and its container is stateless, then, there <em>should be</em> a way to do something like that, shouldn't it?</li>
  </ol>
  
  <h2>Some thoughts</h2>
  
  <p>I've heard about <a href="http://reactphp.org/" rel="nofollow noreferrer">React PHP</a>, <a href="http://socketo.me/" rel="nofollow noreferrer">Ratchet PHP</a> for Websocket integration, <a href="https://icicle.io/" rel="nofollow noreferrer">Icicle</a>, <a href="https://github.com/php-pm/php-pm" rel="nofollow noreferrer">PHP-PM</a> but never experienced them, it seems a bit too complex to me for now (I may lack some concepts about asynchronicity in apps, that's why my brain won't understand until I have some more answers :D ).</p>
  
  <p>Is there any way that these libraries could be used as "wrappers" for our kernel request handling?</p>
  
  <p>I mean, let's create this reactphp/icicle/whatever environment setup, create our kernel like we would do in any Symfony app, and run the app as web-server, and when a request is retrieved, we send it asynchrously to our kernel, and as long as the kernel has not sent the response, the client waits for it, even if the response is <em>also</em> sent asynchrously (from nested callbacks, etc., like in NodeJS).</p>
  
  <p>This would make any existing Symfony app compatible with this paradigm, as long as the app is stateless, obviously. (if the app config changes based on a request, there's a paradigm issue in the app itself...)</p>
  
  <p>Is it even a possible reality with PHP libraries rather than using PHP internal web-server in another way?</p>
  
  <h3>Why ask these questions?</h3>
  
  <p>Actually, it would be kind of a revolution if PHP could implement <em>real</em> asynchronous stuff internally, like Javascript has, but this would also has a big impact on performances in PHP, because of persistent data in our web-server, less bootstraping (require autoloader, instantiate kernel, get heavy things from cached files, resolve routing, etc.).</p>
  
  <p>In my thoughts, only the <code>$kernel-&gt;handleRaw($request);</code> would consume CPU, the whole rest (container, parameters, services, etc.) would be already in the memory, or, for the case of services, "awaiting to be instantiated". Then, performance boost, I think.</p>
  
  <p>And it may troll a bit the people who still think PHP is a very bad and slow language to use :D</p>
  
  <h3>For readers and responders ;)</h3>
  
  <p>If a core PHP contributor reads me, is there any way that internally PHP could be more asynchronous even with a specific new internal API based on functions or classes?</p>
  
  <p>I'm not a pro of all of these concepts, and I hope really good experts are going to read this and answer me!</p>
  
  <p>It could be a great advance in the PHP world if all of this was possible <em>in any way</em>.</p>