<document>
<localtoc/>
<deftable>
The PHP Hello, World
The Java module definition
web.xml configuration
Adding the module
</deftable>

This article shows how to use Quercus, Resin's PHP implementation, to create a module in Java callable from a PHP page.

For purposes of this article, I assume that you are working with Resin 3.0.17 and that the directory housing httpd.exe is /var/www/webapps/ROOT. I will call this directory $webApp.

<web-app xmlns="http://caucho.com/ns/resin"> <servlet servlet-name="resin-php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"/> <servlet-mapping url-pattern="*.php" servlet-name="resin-php"/> </web-app> package example; import com.caucho.quercus.module.AbstractQuercusModule; public class HelloModule extends AbstractQuercusModule { /* ** Notice the careful use of the naming ** convention hello_test. This is done ** in order to prevent name collisions ** among different libraries. */ public String hello_test(String name) { return "Hello, " + name; } } example.HelloModule <?php echo hello_test("World") ?>

In your favorite browser, type:

<example> http://localhost:8080/hello.php

You should see:

<results> Hello, World </results>

The first argument of a Java function may be the `Env`. The `Env` provides access to Quercus resources like printing to the browser and retrieving a PHP ini value. For the API of `Env`, refer to the Quercus javadoc.

<example> package example; import com.caucho.quercus.env.Env; import com.caucho.quercus.module.AbstractQuercusModule; public class HelloModule extends AbstractQuercusModule { /* ** Notice the careful use of the naming ** convention hello_test. This is done ** in order to prevent name collisions ** among different libraries. ** ** @param env provides access to Quercus environment resources ** @param name */ public String hello_test(Env env, String name) { env.println("inside HelloModule hello_test()"); return "Hello, " + name; } }

Now `hello_test` will print "inside HelloModule hello_test()" to the browser before returning.

Quercus does marshaling to and from Quercus Values and Java objects. If a Java function requires a String, Quercus will automatically convert the internal Quercus StringValue to a String. If a Java function returns an `int`, Quercus will create a Quercus LongValue for it.

For other Java Objects like `java.util.Date` that are returned to PHP, Quercus puts them into wrappers to expose their public methods and members to the PHP script. Java Collection, List, and Map instances have the additional ability to be used within a PHP script like any other PHP array.

For more information, see Java Interface.

It is fairly straight forward to create your own modules callable from within a Quercus/PHP page. The above tutorial takes through the steps to create the simple hello world application (without needing to "jar-up" your files).

If you want to change your module in any way, all you have to do is resave the ".java" file in the classes\example directory, and Resin will recompile it for you.

You do not need to restart your web app or Resin. It's just that simple.

</document>