jphp-group / jphp

JPHP - an implementation of PHP on Java VM

Home Page:http://jphp.develnext.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is it possible to use mixed Java/PHP classes in a JPHP project?

NyaomiDEV opened this issue · comments

commented

I've had this question for some time now. It would be awesome to work almost seamlessly with both Java and PHP code, since most useful classes are already written in Java (and rewriting some of them takes some effort) and it's the JVM we're using here.

There are a lot of difficulties to implement this. It is especially difficult to implement anonymous classes and lambdas from Java, for this you need to compile the code at runtime. And this, in turn, will create another problem, the inability to compile for Android, because it should already have only compiled code. On Android, another virtual machine is Dalvik VM, which has completely different standards and formats compared to Open/Oracle JDK.

The current work scheme of the JPHP allows you to run any JPHP code on the Dalvik VM, because it is precompiled in JVM bytecode.

For the same reason, the eval function doesn't work on Android, because it requires compilation at runtime.

commented

I didn't mention inline java code on PHP sources because I sort of knew it was difficult. I was referring specifically to having distinct java and PHP source files that can interact with each other.

It's possible now, you can write own extension. You need write wrapper classes for java classes and register the all in extension. The jphp-gui-ext (javafx extension) was written in the same way (https://github.com/jphp-group/jphp-gui-ext/tree/master/jphp-gui-ext/src-jvm/main/java/org/develnext/jphp/ext/javafx/classes - wrapper classes).

It is also possible to write a package for jppm, which will combine both the php code and java via Gradle plugin. An example of such a package - https://github.com/jphp-group/jphp-dffi-ext.

https://github.com/jphp-group/java-reflection-ext - Java Reflection package allows you to work with java reflection.

@dim-s i have opened a issue about that its easy to do that we will start from the graal simple lang implamentation and add each php function look into #379