Java Bridge

Studio supports Java Object instances. Once the PHP Java object has been instantiated, the PHP variable it was assigned to assumes the properties and methods of the Java class. This enables PHP to take advantage of J2EE services.

 

In addition, Studio 5.5 uses only one instance of the JVM to serve all PHP scripts on a given system (as opposed to one per PHP script). The PHP/Java Bridge also enables interaction with Java objects.

 

By default, Studio 5.5 uses the JRE configuration used to load itself.

Note:

Embedded Java Code Completion is available only in the Professional Edition.

Code Completion

Code Completion offers the following functions:

object java_last_exception_get()

void java_last_exception_clear()

void java_set_ignore_case(bool ignore)

array java_set_encoding(string encoding)

void java_throw_exceptions(int throw)

Assignments

Assignments attach the resulting Java object to the PHP variable, e.g.,:

$a = new java("java.lang.System");

Exclusions

The following Java items are excluded: Integer, Double, String, Boolean, HashTable and Object[], NULL (Java) .

They are mapped to int, float, string, boolean, array and null (PHP) respectively (see the table below):

Java

PHP

java.lang.String

String

java.lang.Integer

int, Long, Byte, Char, Short

 Int

java.lang.Double / double,
Float

Float

java.lang.Boolean / Boolean

Boolean

Object[]

Array

Hashtable

Array

All other objects remain Java objects

Object

Changing the JRE

Follow the instructions below to change the Java Runtime Environment (JRE):

  1. Go to Preferences | Code Completion.

  2. Go to the section labeled Java Bridge (bottom left). Select the Browse button. The Installed JRE dialog will open. It lists the JRE(s) used by Studio to implement the Java Bridge.

  3. Click Add.

  4. Press Add or Edit to open the Installed JREs dialog. Use it to add (or edit) the JRE configuration as required.
    The Remove and the Edit buttons are enabled for all defined JREs except for the JRE definition used by Studio itself.

New Project (Classpaths and JRE)

Classpaths and the JRE can be set for a project when creating a new project. This is done using the New Project Wizard.

  1. Click Project | New Project. The New Project Wizard will appear.

  2. Click Next.
    The Path Entry dialog will appear. Add or delete path entries for your source files.

  3. Click Add to add a path to the desired file(s).

  4. Click Next until the dialog to set the JRE, JARS and Classfolders.

  5. Click New to select a new JRE if required.

  6. Click Add JARs and/or Add Class Folders to select new JARs and/or to add class folders if required.

7.  Click Finish to create the project and finish.

Adding Java Objects

Follow the instructions below to add Java Objects:

  1. Create a project.

  2. Open/create a PHP file.

  3. Create a new Java instance (e.g. $a= new Java("") ).

  4. Put the cursor between the quotation marks and activate Code Completion (CTRL+Space).
    Code completion exists for the default Java packages.

  5. Continue with the Code Completion procedure until the appropriate class has been selected.

  6. The PHP variables that the Java objects are assigned to assume the properties and methods of the Java class (string, integer and Boolean).

Adding New Packages to the Code Completion Library

You can add Classpaths, JARs and folders containing Java files to the project. Doing this enables the methods, classes, functions, etc., that are present in the added packages to be available to the current code completion library.

  1. Go to Project | Project Properties. The Project Properties dialog will open

  2. Select the Java Bridge tab.

  3. Click Add Jars or Add Class Folders. The appropriate Add dialog will open.

  4. Locate the JAR / Class folder. Click Select and OK to add the JAR(s) and return to the IDE.

The added objects now appear in the code completion library.

PHP /Java Integration Code Example

<?

// EJB configuration for JBoss. Other servers may need other settings.

// Note that CLASSPATH should contain these classes

$envt = array(

"java.naming.factory.initial" =>

"org.jnp.interfaces.NamingContextFactory",

"java.naming.factory.url.pkgs" =>

"org.jboss.naming:org.jnp.interfaces",

"java.naming.provider.url" => " jnp://yourflowers.com:1099"

);

$ctx = new Java("javax.naming.InitialContext", $envt);

// Try to find the object

$obj = $ctx->lookup("YourflowersBean");

// here we find an object - no error handling in this example

$rmi = new Java("javax.rmi.PortableRemoteObject");

$home = $rmi->narrow($obj, new Java("com.yourflowers.StoreHome"));

// $hw is our bean object

$store = $home->create();

// add an order to the bean

$store->place_order($_GET['client_id'], $_GET['item_id']);

print "Order placed. Current shopping cart: ";

// get shopping cart data from the bean

$cart = $store->get_cart($_GET['client_id']);

foreach($cart as $item) {

print "$item[name]: $item[count] at $item[price]\n";

}

// release the object

$store->remove();

 

 

Related Links

Related Links:
Zend Studio Components