I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 635 posts at DZone. You can read more from them at their website. View Full User Profile

PHP on a Java application server

11.01.2011
| 6992 views |
  • submit to reddit

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.

Conclusions

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.

Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gabriele Santini replied on Tue, 2011/11/01 - 7:01am

You're absolutely right about Quercus, even if I find it an amazing product. The worst is not when applications simply do not work, is when they seem to work until you discover weird behaviours here and there, due to a slightly different implementation of native PHP...

Giorgio Sironi replied on Sun, 2011/11/06 - 8:08am in response to: Gabriele Santini

There is no specification of the language so it must be difficult for them to replicate the behavior of PHP core. A comprehensive test suite goes a long way to discover these faults.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.