Overview and Requirements
To access pure Java (or .NET) libraries from PHP the following is necessary:
- A VM must be running somewhere on the local network.
- The JavaBridge.jar (or the JavaBridge.war or MonoBridge.exe variant) must be accessible by this VM.
The Java/.NET libraries can be called from any PHP implementation by opening a socket connection to the VM and by sending PHP/Java Bridge protocol requests:
<?php
require_once("http://HOST:PORT/WEB_APP/java/Java.inc");
java_autoload("myApplication.jar;myLibrary1.jar;myJdbcDriver1.jar");
$main = new com_my_application_Main();
$main->doSomething($_POST);
?>
Furthermore PHP libraries are available to Java JSP or IOC Frameworks, provided that the web application's WEB-INF/web.xml contains a context declaration. Example:
engine = EngineFactory.getInvocablePhpScriptEngine(this, application, req, res);
engine.eval ('<?php function f($v) {return 1+(int)(string)$v;} ?>');
Object result = ((Invocable)engine).invokeFunction("f", new Object[]{"2"});
out.println(result);
An interactive PHP script engine is also available:
jrunscript -classpath JavaBridge.jar -l php-interactive
function toString() { return "hello Java from PHP"; }
echo java_closure()
=> hello Java from PHP
It is neither necessary nor recommended to write custom- or glue logic for your Java- or PHP classes.