title: PHP/Java Bridge - Access Java objects in PHP
body: <p>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.</p>
  
  <p>So here's my Java application:</p>
  
  <pre><code>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()
      {
      }
  }
  </code></pre>
  
  <p>From PHP, I would like to get access to the App object that is created and call the method <code>f()</code> on it.</p>
  
  <p>I have tried playing around with this thing called "context". So in the Java method <code>App.appStart()</code>, I did this:</p>
  
  <pre><code>// Save a reference to this App object to be retrieved later in PHP.
  new PhpScriptContextFactory().getContext().put("x", this);
  </code></pre>
  
  <p>And in PHP, I tried to access the saved object like this:</p>
  
  <pre><code>require_once("http://localhost:5080/JavaBridge/java/Java.inc");
  var_dump(java_is_null(java_context()-&gt;get("x")));
  </code></pre>
  
  <p>Unfortunately, the <code>java_is_null()</code> function in PHP returns <code>true</code>.</p>
  
  <p>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 <code>null</code>.</p>