marc-mabe / php-enum

Simple and fast implementation of enumerations with native PHP
BSD 3-Clause "New" or "Revised" License
464 stars 36 forks source link

__toString() should return value by default, not name #33

Closed DmitryVarennikov closed 10 years ago

DmitryVarennikov commented 10 years ago

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 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.

UPD: reference #24

marc-mabe commented 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:

By the way __toString() is not final - you can overwrite it to return the value ;)