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 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 attach the resulting Java object to the PHP variable, e.g.,:
|
|
$a = new java("java.lang.System"); |
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 |
|
java.lang.Boolean / Boolean |
Boolean |
|
Object[] |
Array |
|
Hashtable |
Array |
|
All other objects remain Java objects |
Object |
Follow the instructions below to change the Java Runtime Environment (JRE):
Go to Preferences | Code Completion.
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.
Click Add.
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.
Classpaths and the JRE can be set for a project when creating a new project. This is done using the New Project Wizard.
Click Project | New Project. The New Project Wizard will appear.
Click Next.
The Path Entry dialog will appear. Add or delete path entries for your
source files.
Click Add to add a path to the desired file(s).
Click Next until the dialog to set the JRE, JARS and Classfolders.
Click New to select a new JRE if required.
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.
Follow the instructions below to add Java Objects:
Create a project.
Open/create a PHP file.
Create a new Java instance (e.g. $a= new Java("") ).
Put the cursor between the quotation marks and activate
Code Completion (CTRL+Space).
Code completion exists for the default Java packages.
Continue with the Code Completion procedure until the appropriate class has been selected.
The PHP variables that the Java objects are assigned to assume the properties and methods of the Java class (string, integer and Boolean).
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.
Go to Project | Project Properties. The Project Properties dialog will open
Select the Java Bridge tab.
Click Add Jars or Add Class Folders. The appropriate Add dialog will open.
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: |
|
|
|