|
From: <php...@li...> - 2006-11-03 22:12:33
|
On Nov 3, 2006, at 1:54 PM, php-java-bridge-
us...@li... wrote:
> I'm using php-java-bridge-3.1.8, and have run into something really
> confusing when creating
> HashMaps.
>
> I have a method in a Java class that accepts a HashMap. If I
> create an
> empty HashMap in
> php and add elements with put like this:
>
> $h = new java("java.util.HashMap");
> $h->put(0 , "Some Text");
>
> ...and pass that to my class I can get it's value with get(0) no
> problem.
> However if I create
> the HashMap in php with something like this:
>
> $h = new java("java.util.HashMap", array(0=>"Some Text"));
>
> ...and pass that to the java class it wasn't able to get the key
> 0. Also,
> trying to use $h->get(0)
> within php didn't work either.
Since the PHP array type is not directly compatible with a HashMap -
even though they are basically the same concept of key/value pairs -
you have to be careful when moving values back and forth between the
two. I have found that building a PHP array with all the values I
need first and then using a simple FOREACH loop to populate my Java
HashMap works great. Here's an example:
<?php
...
// build a request array
$request['key1'] = "value1";
$request['key2'] = "true";
$request['key3'] = 2342;
$request['key4'] = "internet";
// == Prepare the request HashMap for Java ==
$j_request = new Java("java.util.HashMap");
foreach ($request AS $key => $val) {
$j_request->put($key,stripslashes($val));
}
// To get your PHP array back from a HashMap just use the following
$original_request_array = java_values($j_request);
...
?>
Since Java is so strongly "typed" I have also run into a lot of
problems with PHP's loose typing and Java.
Hope this helps a bit.
>
> What I found is that the key to the resulting HashMap using the latter
> syntax isn't an
> Integer 0, but rather an Object of the class
> php.java.bridge.Request$PhpArrayKey.
>
> I see that creating a Vector directly from an array in a similar
> manner such
> as:
>
> $h = new java("java.util.Vector", array(44.6, 5, 5.6, "some string"));
>
> ...works fine, and I get elements with all the expected data types
> in java.
>
> Is this the expected behavior and possible something I missed in the
> documentation?
Nope. This is just an issue with data types between PHP and Java. I
don't think you missed anything in the docs. The doc's for the
Bridge don't really discuss the intricacies of handling different
data types between PHP and Java. Maybe that is a good section to add
to the documentation. I'll add that to my list.
Jon Koerber
(spladow)
> It had
> me going for hours. I see that there's at least one other email on
> the list
> were someone
> couldn't read a value with get()...possibly the same issue.
>
> Thanks in advance.
>
> Tom
>
> _________________________________________________________________
> Try the next generation of search with Windows Live Search today!
> http://imagine-windowslive.com/minisites/searchlaunch/?locale=en-
> us&source=hmtagline
>
>
> ----------------------------------------------------------------------
> ---
> Using Tomcat but need to do more? Need to support web services,
> security?
> Get stuff done quickly with pre-integrated technology to make your
> job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache
> Geronimo
> http://sel.as-us.falkag.net/sel?
> cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> php-java-bridge-users mailing list
> php...@li...
> https://lists.sourceforge.net/lists/listinfo/php-java-bridge-users
|