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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.