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() shouldn't be final and return the name by default #24

Closed marc-mabe closed 11 years ago

marc-mabe commented 11 years ago

... this would be the same behavior as in Java

Returning the name makes more sense because it is already a string we like to return to.

@prolic ping

prolic commented 11 years ago

Can you please demonstrate a use case and show why it would be more appropriate?

marc-mabe commented 11 years ago

If you need work with the real "internal" value, you should use getValue() If you need to get the exact name used to declare the enum constant, you should use getName() as __toString() may have been overriden If you want to print the enum constant in a user friendly way, you should use __toString() which may have been overriden. By default the name is the most user friendly value. (echo "Your size is {$sizeEnum}";)

prolic commented 11 years ago

1) I was always the opinion, __toString() should NOT be final. So good for me! 2) I think a very often use-case is something link:

switch($userStatus) {
    case UserStatus::ACTIVE:
        // code
        break;
}

So having the value as default makes sense for me. On the other side your other example:

echo "Your size is {$sizeEnum}";

I can't decide, which one is better, or more useful, it depends on the use case. I think you can go both ways, so I won't disagree, if the default value is the name instead of the value.

marc-mabe commented 11 years ago

1) I was always the opinion, __toString() should NOT be final. So good for me!

Sure, that's why this issue exist ;)

2) I think a very often use-case is something link ...

Your example only works with strings as values. If you set integers you'll get a warning Object of class MabeEnumTest\TestAsset\EnumWithoutDefaultValue could not be converted to int

prolic commented 11 years ago

:+1: