Run PHP Web Applications on the Java Platform

There has been a lot of commotion and even a book with having Ruby on Rails run on the Java VM. But looking past the JRuby hype it is clear that PHP has a several orders of magnitude more open source code, projects, and corporate backing than Ruby on Rails. The folks behind the Resin application server have the ubiquitous of PHP and have developed Quercus, a PHP 5 implementation written in Java.

Quercus is available with the latest Resin but it is also available for download as a WAR file which can be deployed on Resin or other application Servers such as GlassFish.

Getting Started

Get the latest version of Resin server, as of this writing the latest version is Resin 3.1.3. To start the server just run the <INSTALL DIRECTORY>\bin\httpd script from the command prompt. If you look around the Resin install directory you will find a php folder where the Quercus WAR file is already configured to handle any PHP file. To give the Quercus PHP engine a try, lets create a simple test. First lets create phptest directory in <INSTALL DIRECTORY>\webapps. In the phptest directory create a new file, index.php, and add the following text to it.

<html>
<body>
<?php phpinfo(); ?>
</body>
</html>

The phpinfo method outputs a large amount of PHP configuration information. I usually print this information out to ensure that PHP is setup correctly. If you have already started the Resin server you can direct your browser to the following URL: http://localhost:8080/phptest/

You should be looking at a screen that looks like the following screen shot.

phpinfo

Mixing PHP with Java

Since Quercus is written in Java there are hooks to import and mix Java classes in your PHP files. Lets import a Java class and manipulate it in PHP. In the next sample piece of code I will import a Java HashMap class and manipulated it PHP. The import functionality is obviously not part of PHP but an extension made possible by Quercus. .

<html>
<body>
<?php
import java.util.HashMap;

$map = new HashMap();

// Add some name-value pairs
$map->put(’california’, 1000);
$map->put(’oregon’, 1200);

$total = 0;
// Iterate over keys and sum values
for($itr = $map->keySet()->iterator(); $itr->hasNext(); ) {
$key = $itr->next();
$total += $map->get($key);
}
print “Total $total”;

?>
</body>
</html>

Once a Java class has been loaded it can be handled just like any other object in PHP. In addition having access to JDK classes, you can import your own Java code. You can import any class that is available in your web applications class loader.

Run PHP Applications

Using Quercus and Resin you can deploy full fledge PHP web applications such as WordPress or Joomla! To get a PHP application running on Resin with Quercus you just need to unzip the application into the <INSTALL DIRECTORY>\webapps directory. Since your PHP application would be running on top of Java you need to have the right JDBC dirver installed in the <INSTALL DIRECTORY>\lib directory. This is usually all you have to do to have a PHP application to run on JSP/Servlet container like Resin.

There is a list of PHP applications that are known to be running on Java via Quercus.

Quercus opens a lot of opportunities for PHP web development. Now you don’t really have to choose between languages or frameworks, develop in what you know is best for the task and resources at hand and interoperate between PHP, Java, Ruby, Groovy whatever not at the XML level but at the bytecode.

Technorati Tags: , , , , , , , , ,

Enjoy. Share. Be Happy.
  • Twitter
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Tumblr
  • Google Bookmarks
  • FriendFeed
  • Yahoo! Buzz
  • Reddit
  • Digg
  • HackerNews
  • Suggest to Techmeme via Twitter
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • Mixx
  • Furl

Related posts:

  1. Import Script, Import CSS, Import PHP
  2. Java Web Service with HTTPS
  3. Google Web Toolkit Tutorial: The Break Down
  4. Visual Basic And The Java Platform
  5. RAD Frameworks For The Java Platform

This entry was posted in Java, PHP, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. KiLVaiDeN
    Posted October 12, 2007 at 7:11 am | Permalink

    Nice article !

    I think it’s interesting to now think differently about PHP, and use Java as the middletier or persistenttier, and let PHP do the presentation job :)

    I would be interested on how PHP can interact with some apis of Java, specially Hibernate or Spring. Don’t think you can “inject a php page” yet ^^

    Cheers
    KiLVaiDeN

  2. Posted October 12, 2007 at 8:41 pm | Permalink

    nice article, but I prefer to use jetty rather than resin and I’ve been bustin my butt trying to get quercus works on jetty embbeded.

    Can u help me?

  3. Posted October 13, 2007 at 3:38 am | Permalink

    Eh, finally it works, quercus on jetty ;) it turns out that I have to extract the war myself. Thanks anyway.

3 Trackbacks

  1. By napyfab:blog» Blog Archive » links for 2007-10-12 on October 12, 2007 at 6:33 pm

    [...] Juixe TechKnow » Run PHP Web Applications on the Java Platform (tags: php java tutorial webdev web development) [...]

  2. [...] introduzione con [...]

  3. By Discovered Quercus | Über Software on August 17, 2009 at 3:06 pm

    [...] aside, more important to me is actually to test if I can integrate my php app with my JEE apps and share a Persistence layer /ORM cache as well as clustering aspects [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*