|
Instead of just executing a PHP script and calling Java methods from
within such a script we can also call PHP functions and even PHP object methods
from Java. First a litte PHP snippet:
function useless($i) {
return new foo($i);
}
class foo {
var $val = '';
function __construct($i) {
$this->val = $i;
}
function bar($i) {
return 'foo::bar::'.$i.' ('.$this->val.')';
}
}
And a small Java program:
ScriptEngine eng = mgr.getEngineByName("turpitude");
Compilable comp = (Compilable)eng;
CompiledScript script = comp.compile(/*Source*/);
Invocable inv = (Invocable)script;
Object phpobj = inv.invokeFunction("useless", "Function Value");
Object retval = inv.invokeMethod(phpobj, "bar", "Method Value");
Even better: define an Intefarce in Java...
public interface ExampleInterface {
public String bar(String s);
}
ExampleInterface exint;
exint = inv.getInterface(phpobj, ExampleInterface.class);
exint.bar("interface call");
implement it in PHP...
class foo {
var $val = '';
function __construct($i) {
$this->val = $i;
}
function bar($i) {
return 'foo::bar::'.$i.' ('.$this->val.')';
}
}
and call it in Java:
exint = inv.getInterface(ExampleInterface.class);
exint.bar("interface call");
see: InvocableSample.java
|
Comments
Add Comment