January 23, 2021
Hot Topics:

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

  • By Jason Gilmore
  • Send Email »
  • More Articles »

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:


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.


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.

Page 2 of 2

This article was originally published on August 27, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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