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

Class constant visibility in Enum #135

Closed jdecool closed 5 years ago

jdecool commented 5 years ago

Actually the Enum class only manage public constant.

I think it's interesting to manage constant independently of the property visibility.

When I create an Enum, I don't want the developer know how it works internally, so I don't want to expose the constant I used to build it.

Should you open to a PR to make this change ?

marc-mabe commented 5 years ago

Hi @jdecool,

Enumerators are public by definition. As PHP doesn't support real enumeration types this library emulates them as much as possible. For that it's using class constants as workaround.

Here are some reasons why enumerators are public:

jdecool commented 5 years ago

Thanks for your answer.

But I not agree with you.

The Enum should be encapsulate in the object (accessible with MyEnum::ENUMERATOR()->getValue()) and should not directly acces the constant MyEnum::ENUMERATOR used to build it.

marc-mabe commented 5 years ago

Hi @jdecool,

This again is an implementation detail because of the lag of build-in enum support in PHP. An enumerator is a constant definition. This implementation uses objects for this case and makes the defined enumerator value and name accessable. On a perfect implementation both information would be available on compile time but that's not possible here as this library emulates the enumeration type as much as possible. What I want to say is that an enumerator value is just one possible value of a self defined enumeration type. Which makes it public by definition.