Description
Depending on how you choose consts values in your Enum class, it can have very undesirable effects. This is because the library uses in_array
(constructor) and array_search
(new search method) without strict checking. To get the notion of what I mean, please look at: https://ideone.com/FvouJm
The same can happen if you use many other problematic values like null, booleans, empty string etc. While majority of them are impractical to use, having value as integer 0 is. My point is, if you chose to have such a weird values, it's up to you, but the library should never allow to create enums from out of range.
You can really see the problem when you create enums from user input:
- converting query strings params to enums
- creating enums from arguments in php CLI apps
I think it'd be worthwhile to add that to docs too. I have a PR ready with the fix, just needs some styling changes. What do you think ?
tl;dr You can create enums from out of defined values
class MyEnum extends Enum {
const A = 0;
const B = 1;
}
new MyEnum('any string'); // SUCCESS but it should throw \UnexpectedValueException