0

Java:

package test;

public class HelloWorld {
public HelloWorld(String args){
}

public void ppp(){
    System.out.println("....");
};

public  void set(HelloWorld hw){
    hw.ppp();
}

public static final String JAVABRIDGE_PORT = "28080";
static final php.java.bridge.JavaBridgeRunner runner = php.java.bridge.JavaBridgeRunner
        .getInstance(JAVABRIDGE_PORT);

public static void main(String args[]) throws Exception {
    runner.waitFor();
}
}

PHP:

<?php
require_once("/home/gt/workspace/JavaPhp/Java.inc");
use test\HelloWorld;

class abc extends HelloWorld{
function ppp(){
echo "!!!!";
}
}

$hw =new java("test.HelloWorld","str");
$hw -> set(new abc("str"));

You can see in the PHP code a php class extend Java class and the set() method is Java's methods which let PHP class pass to the Java side.

Could it print "!!!!" not "...." ?

I know py4j could do it , wondering how PHP/Java Bridge do it?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
DunkOnly
  • 1,682
  • 4
  • 17
  • 39

1 Answers1

0

you can implement it using a custom php5/php7 __autoload function which checks each argument and, if it is an instanceof Java, wraps it using java_closure() with a list of generated interfaces for the wrapped Java type. In pseudo-code something like:

java_autoload_function() {...
__call(java_closure($type, null, $Class->getInterfaces($type)))
}

Needs not more than ~10-15 lines of additinal PHP code in Java.inc.

user1135940
  • 126
  • 1
  • 4