PHP/Java Bridge - Access Java objects in PHP
- by Omer Hassan
I have a Red5 application which defines some public Java methods. When I start the server, an object of the application class gets created. I am trying to call the public methods of the application class from PHP using the existing instance of the Java application class.
So here's my Java application:
public class App extends org.red5.server.adapter.ApplicationAdapter
{
public boolean appStart(IScope app)
{
// This method gets called when the application starts
// and an object of this App class is created.
return true;
}
// This is the method I would like to call from PHP.
public void f()
{
}
}
From PHP, I would like to get access to the App object that is created and call the method f() on it.
I have tried playing around with this thing called "context". So in the Java method App.appStart(), I did this:
// Save a reference to this App object to be retrieved later in PHP.
new PhpScriptContextFactory().getContext().put("x", this);
And in PHP, I tried to access the saved object like this:
require_once("http://localhost:5080/JavaBridge/java/Java.inc");
var_dump(java_is_null(java_context()->get("x")));
Unfortunately, the java_is_null() function in PHP returns true.
I also tried saving the App object in a static variable of App class but when I access that variable in PHP, its value is null.