{"id":68,"date":"2007-11-19T12:28:07","date_gmt":"2007-11-19T11:28:07","guid":{"rendered":"http:\/\/blogs.igalia.com\/eocanha\/?p=68"},"modified":"2015-11-08T00:21:08","modified_gmt":"2015-11-07T23:21:08","slug":"idea-to-build-an-application-server-in-php","status":"publish","type":"post","link":"https:\/\/eocanha.org\/blog\/2007\/11\/19\/idea-to-build-an-application-server-in-php\/","title":{"rendered":"Idea to build an application server in PHP"},"content":{"rendered":"<p>Theese days I&#8217;ve been following a thread in the php-es mailing list at lists.php.es. A subscriber noticed that static class attributes weren&#8217;t shared between different page requests. Someone explained him that the way PHP deals with the requests is the cause for it not to have an &#8220;application&#8221; variable scope.<\/p>\n<p>In Java servlets, for example, static class attributes are shared by all the instances of that class in the virtual machine. Scalability (and speed) at a medium scale is achieved using object in-memory caches. In PHP that approach can&#8217;t be done without using shared memory management libraries, and never in an easy way. Developers have to resort to disk and database caches, much slower that the memory ones.<\/p>\n<p>Moreover, the fact of having to parse in each request not only the target PHP file, but also all those ones recursively included, represent another of the disadvantages of the platform.<\/p>\n<p>It&#8217;s strange because, as far as I know, an &#8220;execution environment&#8221; common to all the requests does exist on other languages, like Perl (Apache mod_perl) or Python. That way, data can be shared among the requests, memory caches can be done, and so on.<\/p>\n<p>Something that would be nice, even being a daydream, would be to develop an &#8220;application server&#8221; consisting of:<\/p>\n<ul>\n<li>A library that made shared memory usage simpler for storing session variables and object instances on it (even singletons), making them available from different threads.<\/li>\n<li>A multithreaded web server implemented entirely in PHP using the socket API. The server would spawn threads with each request using the threads and processes API. It&#8217;d be a replacement for Apache.<\/li>\n<\/ul>\n<p>This way, we would have the advanteges of the application servers and the syntax simplicity of PHP. The disadvantage would be that, as PHP has been developed with the &#8220;parse, create environment, process the request and dispose everything&#8221; model in mind, with the proposed approach, a great number of memory leaks would be found. That memory leaks are currently hidden thanks to the short life cycle of the requests.<\/p>\n<p>That&#8217;s all. This was an idea I had some time ago and it seemed interesting to me. Now I&#8217;ve decided to share it with you. It&#8217;s surprising that nobody has already thought about it, isn&#8217;t it?<\/p>\n<p><strong>UPDATE:<\/strong><\/p>\n<p>I&#8217;ve just found <a href=\"http:\/\/quercus.caucho.com\/\">Quercus<\/a>, a PHP5 implementation written in Java that allows PHP programs to interact directly with Java libraries, taking advantage of all the benefits that Java caches, connection pools, etc. can provide. It&#8217;s not the same approach I propose, but maybe a good one anyway.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Theese days I&#8217;ve been following a thread in the php-es mailing list at lists.php.es. A subscriber noticed that static class attributes weren&#8217;t shared between different page requests. Someone explained him that the way PHP deals with the requests is the cause for it not to have an &#8220;application&#8221; variable scope. In Java servlets, for example, &hellip; <a href=\"https:\/\/eocanha.org\/blog\/2007\/11\/19\/idea-to-build-an-application-server-in-php\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Idea to build an application server in PHP<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/68"}],"collection":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":1,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":434,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions\/434"}],"wp:attachment":[{"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eocanha.org\/blog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}