PHP 4.0 Bug #9162 Updated: Java support corrupts Hashtables
| From: | freifeld at turbogenomics dot com | Date: | Thu, 08 Feb 2001 15:58:27 +0000 |
| Subject: | PHP 4.0 Bug #9162 Updated: Java support corrupts Hashtables | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-46034@lists.php.net to get a copy of this message | ||
ID: 9162
User Update by: freifeld@turbogenomics.com
Status: Open
Bug Type: Class/Object related
Description: Java support corrupts Hashtables
Just in case you were confused, the java method getSomething() should return (String) myhash.get(
"myname" ), and the PHP call should be my_java->getSomething( $my_hash ). You get the
idea.
Previous Comments:
---------------------------------------------------------------------------
[2001-02-07 15:28:44] freifeld@turbogenomics.com
This bug is with the Java support in PHP4. PHP seems to try to treat Java Hashtable objects as
PHP-native arrays, with several negative impacts. First of all, Java Hashtables have no internal
concept of ordering or null termination, nor do they have an internal "current" pointer.
Therefore, if you try to travserse a Java Hashtable as you would a PHP array, you will run off the
end! However, since the Hashtable is not an object (under PHP), you cannot call $my_hash->get(
"x" ) or $my_hash->put( "y" ).
Second of all, if you try to pass such a Hashtable BACK to a Java class method, you will find that
the Hashtable has been corrupted, and all your datatypes changed and you will get various
NullPointerExceptions or illegal cast exceptions.
It seems to me the most natural approach at this point would be to just treat Java Hashtables as
generic objects, and therefore retain the get() and put() methods. That way you don't corrupt
the Hashtable, and you don't let anyone go off the end of any arrays.
I don't know if this kind of thing happens with other Java object classes.
There is a workaround whereby one can, in Java, explicitly repair a corrupted Hashtable, but that is
pretty ugly. If you are interested in looking at the workaround, email me.
Here is a code snippet illustrating what I mean:
MyClass.java:
public class MyClass {
public MyClass() { }
public Hashtable makeHash( int ikey, int ival, String skey, String sval ) {
Hashtable myhash = new Hashtable();
myhash.put( ikey, ival );
myhash.put( skey, sval );
myhash.put( "myname", "joe" );
return myhash;
}
public String getSomething( Hashtable myhash ) {
return myhash.get( "myname" );
}
}
MyPage.php
<?
$my_java = new Java( "MyClass" );
$my_hash = my_java->makeHash( 1,2,"yourname", "steve" );
// watch this:
print( implode( ",", $my_hash ) ); // explosion! off the end of the array
// or,
print( $my_java->getSomething() ); // java exception!
?>
---------------------------------------------------------------------------
Full Bug description available at: http://bugs.php.net/?id=9162