Usability Issues

The Java Bridge’s PHP 4 module has a number of usability issues compared to native Java code, which stem from the limitations imposed by the PHP 4 language. Most of these limitations do not exist in PHP 5 and those that do will be mentioned as such.

Chain Functions Call

In pure Java, you can program the following chain function:

result = object.Method1().Method2().Method3();

In this example, the result of one method becomes the object for another method. PHP 4’s Java module, however, does not allow you to write a chain function in a similar way, as per example:

$result = $java_object->Method1()->Method2()->Method3();

This is due to the fact that PHP 4 disallows chaining method calls. Instead, a chain function must be expressed as follows:

$result1 =  $java_object->Method1();

$result2 = $result1->Method2();

$result = $result2->Method3();

Note:

In PHP 5, you can use chaining.

Exceptions

Since PHP 4 has no concept of exception, you may not include Java exceptions in your PHP code. However, you can use functions to deal with exceptions.

java_exception_get:

http://www.zend.com/manual/function.java-last-exception-get.php

java_last_exception_clear:

http://www.zend.com/manual/function.java-last-exception-clear.php

PHP 5 has a concept of exceptions and therefore can handle Java exceptions and translate them into PHP exceptions.

The following examples display the different exception scenarios and what they look like:

How Exceptions Work:

In this example exceptions are inherited from an exception class.

Usage Example

Example:

<?

try {

  $stack=new Java("java.util.Stack");

  $stack->push(1);

  $result = $stack->pop();

  print "$result\n";

  $result=$stack->pop();

} catch(Exception $ex) {

  print "Exception in pop: ";

  print $ex->getCause()->toString();

  print "\n";

}

?>

 

Caught Exceptions:

This example shows what an exception looks like when a Java Code exception is caught. This is an example of a typical exception that will appear instead of the expected PHP output when specified in the code i.e using print_r ($exception) or var_dump ($exception).

Usage Example

Example:

JavaException Object

(

    [message:protected] => Java Exception java.util.EmptyStackException:

java.util.EmptyStackException

        at java.util.Stack.peek(Stack.java:79)

        at java.util.Stack.pop(Stack.java:61)

    [string:private] =>

    [code:protected] => 0

    [file:protected] => /vector.php

    [line:protected] => 7

    [trace:private] => Array

        (

            [0] => Array

                (

                    [file] => /vector.php

                    [line] => 7

                    [function] => pop

                    [class] => java.util.Stack

                    [type] => ->

                    [args] => Array

                        (

                        )

                )

        )

    [javaException] => java.util.EmptyStackException Object

)

 

Uncaught Exceptions:

Uncaught exceptions are also reported but because they lack an immediate relation to a specific exception class they are less detailed and can only indicate basic details regarding the occurrence of an exception. This type of exception typically appears in your error log or wherever you have defined your php.ini to store errors.

Note:

Please refer to the PHP manual for more information regarding the php.ini eror reporting definitions.

Usage Example

Example:

Fatal error: Uncaught exception 'JavaException' with message 'Java

Exception java.util.EmptyStackException:

java.util.EmptyStackException

        at java.util.Stack.peek(Stack.java:79)

        at java.util.Stack.pop(Stack.java:61)

' in /vector.php:7

Stack trace:

#0 /vector.php(7): java.util.Stack->pop()

#1 {main}

  thrown in /vector.php on line 7

 

Java Array/Hashtable Objects

In PHP, arrays and hashtables are used interchangeably. This is because in PHP hashtables are indexed by integers or strings-not by objects. In Java, the key and value must be objects to be associated, so primitive types have to be converted to objects first, before parsing.

In Zend Platform’s Java interface, if a method returns array/hashtable, it is immediately translated into a PHP native array/hashtable type. This means that if you want to work with a Java array/hash from PHP you cannot preserve it as a Java object. Of course, the contents are preserved, but the object identity is lost. In such a case, when an array/hash is returned, you will lose the ability to use Java methods since the array/hash loses the object identity and becomes a regular PHP array.

There are several ways to handle Java arrays and hashtable descendants. The following example shows a possible scenario of how Java arrays and hashtable descendants can be converted into PHP arrays by splitting the class pattern method and returning an array of strings which is then converted into a PHP array as follows:

Usage Example

Example:

<?

$exp = new Java("java.util.regex.Pattern");

$p = $exp->compile(":+"); // Create new patten object

$arr = $p->split("a::b:c:::d:e"); // Use pattern to split string into array

print_r($arr);

?>

To deal with Array/Hashtable objects originating in Java:

Implement the code dealing with the array in Java and then call it from PHP, or encapsulate the object in a different class.

Iterators

Iterators are not handled by the Java Bridge in any special way and they are treated like any other Java object.

 

 

Related Links

Related Links:
Java Bridge

About

Operating and Configuring

Java Bridge Tab

Common Tasks