There are two possible ways to bridge PHP and Java: you can either integrate PHP into a Java Servlet environment, which is the more stable and efficient solution, or integrate Java support into PHP. The former is provided by a SAPI module that interfaces with the Servlet server, the latter by this Java extension.
The Java extension provides a simple and effective means for creating and invoking methods on Java objects from PHP. The JVM is created using JNI, and everything runs in-process.
This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.
This » PECL extension is not bundled with PHP.
In PHP 4 this PECL extensions
source can be found in the
ext/ directory within the
PHP source or at the PECL link above.
In order to use these functions you must compile PHP with Java support by
--with-java[=DIR] where DIR
points to the base install directory of your JDK. This extension can only
be built as a shared extension. Additional build extensions can be found
Windows users will enable
php.ini in order to use these functions.
In PHP 4 this DLL resides in
extensions/ directory within the PHP Windows
The DLL for this PECL extension
may be downloaded from either the
» PHP Downloads page or from
In order to enable this module on a Windows environment with PHP <=
4.0.6, you must make
jvm.dll available to your
systems PATH. No additional DLL is needed for PHP versions > 4.0.6.
The behaviour of these functions is affected by settings in
For further details and definitions of the PHP_INI_* constants, see the Appendix I,
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
// demonstrate property access
echo 'Java version=' . $system->getProperty('java.version') . '<br />';
echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
echo 'OS=' . $system->getProperty('os.name') . ' ' .
$system->getProperty('os.version') . ' on ' .
$system->getProperty('os.arch') . ' <br />';
// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
echo $formatter->format(new Java('java.util.Date'));
// This example is only intended to be run as a CGI.
$frame = new Java('java.awt.Frame', 'PHP');
$button = new Java('java.awt.Button', 'Hello Java World!');
$frame->visible = True;
$thread = new Java('java.lang.Thread');
new Java()will create an instance of a class if a suitable constructor is available. If no parameters are passed and the default constructor is useful as it provides access to classes like
java.lang.Systemwhich expose most of their functionallity through static methods.
print $date.timewill first attempt to be resolved as
$date.getTime(), then as
java.lang.Class, then static members of the class (fields and methods) can be accessed.
Exceptions raised result in PHP warnings, and
NULL results. The
warnings may be eliminated by prefixing the method call with an
"@" sign. The following APIs may be used to retrieve and reset
the last error:
Overload resolution is in general a hard problem given the differences in types between the two languages. The PHP Java extension employs a simple, but fairly effective, metric for determining which overload is the best match.
Additionally, method names in PHP are not case sensitive, potentially increasing the number of overloads to select from.
Once a method is selected, the parameters are coerced if necessary, possibly with a loss of data (example: double precision floating point numbers will be converted to boolean).
The Java Servlet SAPI builds upon the mechanism defined by the Java
extension to enable the entire PHP processor to be run as a servlet.
The primary advantage of this from a PHP perspective is that web servers
which support servlets typically take great care in pooling and reusing
JVMs. Build instructions for the Servlet SAPI module can be found in