You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I actually disagree that __toString() should return the name by default.
In JAVAenums"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 in switch 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.
__toString() have to return a string but constant values don't need to be strings which would result in a type-cast.
The values are unique using strict comparison. This could result in exactly the same strings for different values after type-cast.
The example switch statement isn't valid because:
It triggers notices Object of class MyIntEnum could not be converted to int
you simply should use the result of 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 comparison
By 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
The text was updated successfully, but these errors were encountered: