http://www.developer.com/

Back to article

Quercus: Bring Java's Power to Your PHP Development (and Vice Versa)


August 26, 2010

Imagine the possibilities when a PHP developer can continue using the language's templating capabilities while leveraging Hibernate's object-relational features. Thanks to an open source project named Quercus, PHP and Java developers alike can mix and match the respective capabilities of both languages.

Quercus is a Java-based implementation of the PHP language, which allows PHP developers to continue using the familiar PHP syntax of the native C-based version just as they always have while plugging into any of the hundreds of Java-based enterprise technologies such as Hibernate and Spring. This approach (part of the movement that software architect Neal Ford famously defined as "polyglot programming") opens up a whole new world of opportunities for developers seeking to squeeze every last ounce of performance and capability from their application platforms.

In this tutorial I'll show you how to install and configure Quercus, and demonstrate how easy even a Java neophyte such as myself can begin using PHP and Java in a seamless fashion. As Ford declared in 2006, the "time of writing an application in a single general purpose language is over."

Installing Quercus

Quercus is bundled as part of the powerful Resin application server, meaning you'll actually need to install Resin in order to get started using Quercus. Resin requires JDK version 1.6, so be sure to check whether your Java environment meets this requirement before proceeding. Then, head over to the Resin download page to retrieve the latest version of the software.

Presuming you're running a Unix-based distribution, installation is as easy as running the familiar configure-make-make install sequence. After installation, you'll need to start the Resin application server by issuing the following command from within the Resin directory:

%>java -jar lib/resin.jar start Resin/4.0.9 launching watchdog at 127.0.0.1:6600 Resin/4.0.9 started -server '' for watchdog at 127.0.0.1:6600

If when attempting to start Resin you receive an error regarding the need to have a "compiled JNI" (as I did on Ubuntu 9.10), see this useful blog post.

When the server has started, open a browser and navigate to http://127.0.0.1:8080. You should be greeted with the Resin default home page and links to the documentation and administration interface.

Executing a PHP File

If Resin started properly, you are ready to begin using Quercus. Test it out by navigating to your Resin installation directory's webapps/ROOT directory and creating a file named phpinfo.php. In it, add the following code:

<?php phpinfo(); ?> 

Call the file within the browser (http://localhost:8080/test.php) and you'll see Quercus' own version of the output returned from the phpinfo() function.

Integrating Java Libraries

As was demonstrated with the previous example, Quercus' syntax is identical to that found in the native distribution. Go ahead and execute a few other PHP functions within the script for good measure. Additionally, Quercus supports the Java import statement, which allows you to import Java libraries into your PHP scripts and then use PHP's object-oriented syntax to integrate those libraries into your code. Perhaps the easiest example involves using Java's Calendar class:

<?php import java.util.Calendar; $calendar = Calendar::getInstance(); echo $calendar->get(Calendar::YEAR); ?>

Executing this example produces the following output:

2010

As you can see from this example, when imported Java's Calendar class features are made available to PHP and accessible through PHP's native object-oriented syntax. This means that while you won't be able to copy native Java code directly into your PHP scripts, in cases such as this the changes really are minimal.

Integrating PHP with iText

Of course, PHP already offers pretty capable date- and time-related features of its own through the DateTime extension. So let's consider another example, this time implementing a feature that PHP alone is incapable of easily accomplishing.

iText PDF is a powerful open source Java library capable of generating and manipulating PDFs in ways that rival the most sophisticated commercial solutions. While PHP has long offered a capable PDF extension, this extension lacks features such as the ability to add watermarks and encryption, two features that are available to iText users. Using Quercus it's trivial to take advantage of iText within your PHP applications!

Begin by downloading the iText JAR file from itextpdf.com and saving it within your Java installation's class path. From there it's just a matter of importing a few native and iText-specific class libraries into your PHP script and then taking advantage of the iText API to create a PDF, as demonstrated here:

<?php import java.io.*; import com.itextpdf.text.*; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; $d = new Document(PageSize::A4); $fos = new FileOutputStream("/home/wjgilmore/src/resin-4.0.9/webapps/ROOT/pdfs/test.pdf"); $pdf = PdfWriter::getInstance($d, $fos); $d->open(); $p = new Paragraph("Quercus is really cool."); $d->add($p); $d->close(); ?> 

Run this script on your Resin server (changing the path passed to the FileOutputStream constructor), and a PDF containing the paragraph will be saved to your server! From there it would be easy to use native PHP code to make the newly generated PDF available to the user.

Conclusion

PHP and Java both sport massive libraries. When combined it seems there's nothing this duo couldn't accomplish. Are you using Quercus within your own applications? If so, tell us about your experiences in the comments!

About the Author

Jason Gilmore is is the founder of the publishing and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Third Edition". Follow him on Twitter at @wjgilmore.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date