Closed DmitryVarennikov closed 10 years ago
In JAVA enums "constants" can have NO value
That's not true. each enumeration has a value you can set it or leaf it but than Java takes the next integer value for you.
As already noted in #24:
__toString()
have to return a string but constant values don't need to be strings which would result in a type-cast.Object of class MyIntEnum could not be converted to int
MyIntEnum::getValue()
to init the switch statement else you get a big performance lost because PHP would automatically call the method and convert the value two time for each comparisonBy the way __toString()
is not final - you can overwrite it to return the value ;)
I actually disagree that
__toString()
should return the name by default.In JAVA enums "constants" can have NO value which is certainly invalid for PHP. And this is not the only difference. You can associate several values with a constant (and that's how usually enums are used in JAVA world). Regarding this it explains why things work this way in JAVA.
I believe for PHP we should adopt this structure and not just copy it blindly. The original argument stated by @prolic regarding usage of
Enum
inswitch
case is valid and I don't see any error. Moreover because we use constants in PHP to emulate enums I strongly expect to see a constant value casting Enum to string and not a constant name. This is expected and clear behaviour.UPD: reference #24