PHP hello world module (jar version)

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
Line 13: Line 13:
  
 
Here is the most important thing to remember when creating a module:
 
Here is the most important thing to remember when creating a module:
* The functions exposed in the module are available from within any PHP application running under Resin.  So you need to be EXTREMELY careful about naming collisions.  In fact, many of the existing PHP modules follow a naming convention where each function name is preceded by the module name and an underscore (IE: mysql_connect).  The module will be callable from within Resin's implementation of PHP whether or not you follow a careful naming convention, but ignore this warning AT YOUR OWN RISK.
+
* The functions exposed in the module are available from within any PHP application running under Resin.  So you need to be EXTREMELY careful about naming collisions.  In fact, many of the existing PHP modules follow a naming convention where each function name is preceded by the module name and an underscore (IE: mysql_connect).  The module will be callable from within Resin's implementation of PHP whether or not you follow a careful naming convention, but ignore this warning at your own risk.
  
 
The tutorial [[PHP Hello World Class Version]] discusses the alternative way of incorporating java into your PHP application.
 
The tutorial [[PHP Hello World Class Version]] discusses the alternative way of incorporating java into your PHP application.

Revision as of 15:15, 6 December 2005


Contents

Introduction

One of the advantages of using Resin's implementation of PHP is that you can create your own library of functions written in Java and access them from within PHP.

There are two different ways of including java code in your PHP application running from within Resin. You can create either:

  1. a module, or
  2. a plain old java object.

There is very little difference in the way the two are constructed, but there is a BIG difference in how they are meant to be used from within your PHP application.

If you want to create a library of functions callable from anywhere within your PHP code, then you should create a module.

Here is the most important thing to remember when creating a module:

  • The functions exposed in the module are available from within any PHP application running under Resin. So you need to be EXTREMELY careful about naming collisions. In fact, many of the existing PHP modules follow a naming convention where each function name is preceded by the module name and an underscore (IE: mysql_connect). The module will be callable from within Resin's implementation of PHP whether or not you follow a careful naming convention, but ignore this warning at your own risk.

The tutorial PHP Hello World Class Version discusses the alternative way of incorporating java into your PHP application.

The article assumes that you have already properly installed Resin on your computer. Please see the related article PHP Hello World for more information on installing Resin for the first time.

The example that follows walks you through the steps of creating a module and also of "jar-ing" up the relevant files.

You should not be put off by all the small steps involved in this process for 2 reasons.

  1. You do not have to "jar-up" the files. See the tutorial PHP Hello World Module - non-jar version for those steps.
  2. There is an extremely popular free utility called Ant available at http://ant.apache.org/. We include at the end of this tutorial the build.xml file you will need to create this jar. Ant takes care of all the nasty little details like creating directories and copying files for you. If you are going to do any serious java development, you probably should just learn how to use Ant at some point anyway.

Step 1: Create Example.jar and place it in the appropriate directory

Create your development environment

The steps that follow assume you are working in a Windows environment. If you are working within Unix (or Linux or Mac), the same steps will work. Just be sure to modify the commands where necessary.

c:\Documents and Settings\Charles >cd c:\
c:\> md sandbox
c:\sandbox> md src
c:\sandbox> md classes
c:\sandbox> cd src
c:\sandbox\src> md example
c:\sandbox\src> md META-INF
c:\sandbox\src> cd META-INF
c:\sandbox\src\META-INF> md services

Create HelloWorldSampleClass.java

Copy the following file into your favorite text editor (notepad will do) and save it to the c:\sandbox\src\example directory.

HelloModule.java

package example;

import com.caucho.quercus.module.AbstractQuercusModule;

public class HelloModule extends AbstractQuercusModule {

  public String hello_test(String name)
  {
    return "Hello, " + name;
  }
}

Create com.caucho.quercus.QuercusModule

Copy the following one line file and save it to the c:\sandbox\src\META-INF\services directory. Make sure that the file name is exactly "com.caucho.php.PhpModule"

com.caucho.quercus.QuercusModule

example.HelloModule

Compile and Jar the files

Navigate to c:\sandbox

C:\sandbox>javac -d classes -classpath c:\resin-3.0.13\lib\quercus.jar src\example\*.java

Note: In my installation of Resin, the file httpd.exe is located in c:\resin-3.0.13. If you have installed Resin in a different directory change the above classpath appropriately.

Create a META-INF\services directory within classes and copy com.caucho.quercus.QuercusModule.

c:\sandbox> cd classes
c:\sandbox\classes> md META-INF
c:\sandbox\classes> cd META-INF
c:\sandbox\classes\META-INF> md services
c:\sandbox\classes\META-INF> cd services
c:\sandbox\classes\META-INF\services> copy ..\..\..\src\META-INF\services\com.caucho.quercus.QuercusModule .

Change directory to c:\sandbox and run the jar utility.

c:\sandbox\classes\META-INF\services> cd c:\sandbox
c:\sandbox> jar cvf example.jar classes

Copy Example.jar to your working resin directory

Step 2: Create and browse the PHP page

javahello.php

<?php

$name = hello_test("Charles");
echo $name;

?>

Conclusion

Enjoy

Personal tools