PDA

View Full Version : Accessing java enums


sirpy
03-20-2011, 02:04 PM
i have the following enum defined inside a java class X

public static enum AttrType {
D {
private final int value = 1;
public boolean isType(int type) { return (type&this.value)>0; }
public int getValue() {
return value;
}
},
Q {
private final int value = 2;
public boolean isType(int type) { return (type&this.value)>0; }
public int getValue() {
return value;
}
}
}

how do I use it from inside php?i've tried X->AttrType X::AttrType, but without any luck

zalmolksis
03-28-2013, 03:43 PM
Hello ! I know it's been two years since you posted that message , but did you find the answer ?
now i have the same problem how to access method from enum

nam
03-29-2013, 06:54 AM
Hi guys,

I just fixed this for 4.0.36.

You'll be able to access nested Enum constants using the '::' scope operator. See http://bugs.caucho.com/view.php?id=3898.

zalmolksis
03-29-2013, 09:39 AM
The problem is a bit different ... the problem is I can't access the methods of enums
exemple:
public enum Gender {
man('strong'), woman('beautifull')

private final String desc;
private Gender(String desc){
this.desc = desc;
}

public String getDesc(){
return desc;
}
}

I have functions that return Gender enums man or woman in php I get string variables and i can't call method getDesc
what I tried :
$enum = java_class('Gender');
import Gender;
var_dump(Gender::man); // resource(man)
var_dump($enum::man); // resource(man)
var_dump(Gender::valueOf('man')); // string(3) "man"

foreach ($enum->values() as $e) {
var_dump($e); // string value
}

When i have resource for exemple Gender::man I can call Gender::man::getDesc()

The problem is that if a method in java returns Gender enum it is serialized in php as string and from string I can't make a resource

Sorry for my english, i hope you will understand me.

PS: Thanks nam for quick reply

nam
03-29-2013, 10:27 AM
Thanks for the clarification. I understand the problem. I'll submit a fix shortly.

nam
03-29-2013, 10:43 AM
Fixed for 4.0.36. See http://bugs.caucho.com/view.php?id=5411. I will commit the changes to our public svn repository in a few hours.

zalmolksis
03-30-2013, 09:23 PM
It's working !
Thank you nam !