Developer

10 tips for PHP scripts: Integrate with existing Java applications

With PHP you can invoke methods of existing Java objects, which lets you integrate PHP into existing Java-based applications. Find out how.

By Julie Meloni
(2/6/01)

Another fancy bit of PHP functionality is its ability to invoke methods of existing Java objects, letting you integrate PHP into existing Java-based applications. This ability is pretty snazzy if you're pushing PHP in your workplace and the answer you get is, "But everything's Java here."

To utilize this functionality, you need to have a Java Virtual Machine (JVM) installed on the server. If you install (or have installed) JDKs from Sun, Kaffe, IBM, or Blackdown, you'll be up to speed.

When you configure PHP, you'll need to add —with-java to the configuration directives, then modify some elements of your php.ini file. The php.ini modifications are usually along the lines of adding the following:

[Java]
java.library.path=/path/to/library
java.class.path=/classpath/
extension_dir=/path/to/extensions
extension=libphp_java.so

Please note, however, that these modifications depend on your type of installation. You should read the README in the ext/java directory in your PHP installation directory to learn more about configuring for Java functionality.

Here's a very simple example of a PHP script creating a new Java object. The script will then access and print to the screen certain Java properties. It's about as exciting as the COM example, but it gives you an idea of the possibilities.

<?

$system = new Java("java.lang.System");
echo "<P>Java version = " . $system->getProperty("java.version") . "<br>";
echo "Java vendor = " . $system->getProperty("java.vendor") . "</p>";

?>

If you have Java knowledge, by all means jump in and help the developers with this project. These types of integration capabilities will be key in the future growth and acceptance of PHP, so the more people working on these types of things, the better.

Julie Meloni is the technical director at i2i Interactive and is an avowed proponent of Linux and the open source community. A regular contribtor to CNET Builder.com, she has written a few books on PHP and other technologies.

Editor's Picks

Free Newsletters, In your Inbox