PHP on a Java application server

DZone 's Guide to

PHP on a Java application server

· Integration Zone ·
Free Resource

Let's say we want to run PHP but we have only an available hosting for Java. An example provided from Resin documentation would be to have an easy way to improve performance, or to gain distributed sessions support for PHP.

A very realistic example would be for the integration of Java and PHP applications: with Resin's Caucho, you can actually run Java and PHP code in the same script. For example, you could take a library available only for one of the two languages and use it on the other side without resorting to different processes that communicate.

The solution that was proposed to my team for running PHP code in a Java environment is Quercus, a module for the Resin application server.

Hello world

Resin is an application server which comes complete of a web front end. We downloaded the latest 4.x version and placed in the ROOT webapp folder a WEB-INF/resin-web.xml file for configuration.

The default proposed behavior is to forward any request ending in .php to an interpreter, but the matcher could just be * if you only need to serve PHP files.

A note for PHPers: servlets are roughly the equivalent of action controllers in our MVC frameworks. Each request for a PHP file is intercepted by the servlet shipped in Quercus, which is able to read the complete request and open the file for execution.

The Hello World example indeed works after only taking a few seconds for configuration. Wonderful, given the complexity of translating a language into another.

Let's try our test suite...

Since PHP has no language specification but just a single implementation, there is no guarantee that the PHP core's would be identical to Quercus in behavior.

We had to be sure that nothing broke by running the test suite of the application, to ensure there were no corner cases in the behavior of array_search() or query size limit in PDO, or any other departure from the official PHP interpreter.

Fortunately, Quercus does not only run PHP code through its servlet, but also via the command line:

java -cp resin.jar script.php

We can use this command to start PHPUnit. After all, /usr/bin/phpunit always links to a PHP file:

java -cp resin.jar /usr/bin/phpunit

The default include_path was .:/usr/share/php, which is exactly like ours include_path on Ubuntu. So no problem with including PHPUnit files or other PEAR stuff.

Here come the problems

However, we found out that some of the classes are implemented in userland (with PHP code instead of with compiled Java code). FilterIterator for example is a class shipped by the PHP core but added as a .php file by Quercus. This kind of implementation may impact performance if your code heavily relies on SPL data structures.

Moreover, at least FilterIterator was not available in the cli version. This could be fixed as we run a pair of class_exists() through the web interface and it found the files. To continue the experiment, we tried even to run PHPUnit in the browser with Matt Mueller's PHPUnit Test Report. We downloaded the package and placed it in the document root only to discover that...

Some classes are missing

Some classes are really missing: RecursiveDirectoryIterator, part of the Standard PHP Library and a dependency of Doctrine 2 and Zend Framework, is not implemented.

We found out there is a fork of Quercus maintained by CleverCloud, which supports more classes and has an open ticket recognizing the need for completing the SPL implementation. The implementations are pure Java this time. Unfortunately, RecursiveDirectoryIterator is not in the relevant branch.

We wrote to the sales team to see if the pro version has better support, but probably it will be focused on non functional requirements like performance.


Resin is a famous and reliable application server for Java applications. However, many of the famous testimonials such as salesforce.com are based on Java technologies and not on its PHP support.

You can easily integrate Java and some new lines of PHP code with Quercus: the setup is really easy. But don't assume that you will be seamlessly able to run an existing PHP application on Resin out of the box: we were unable to run PHPUnit, or an application based on Doctrine2 due to missing implementations. This is the current state of Quercus, but we hope the support improves to furnish PHP developers with yet another option.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}