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

Invalid doc blocks regarding serialization #159

Open ghost opened 7 months ago

ghost commented 7 months ago

Detailed description:

The methods in Enum class related to serialization have the following sentence in the doc block: Enums are not serializable because instances are implemented as singletons. When I first heard of enums and started using them with the help of this library and read I this sentence, I thought this describes the nature of enums, i.e. enum as a data type is unserializable by definition. However after some time, when I gained more programming knowledge I know that enums are serializable even though they are singletons but this cannot be implemented in pseudo-enum implementation via PHP classes.

Expected behavior: Change the doc block to something like "Serialization is not supported as in this pseudo-enum implementation there is no way to unserialize and keep the enum cases as singletons at the same time." (feel free to reword).

marc-mabe commented 7 months ago

Hi @javaDeveloperKid,

Thanks for your report.

The doc-block comment refers to the class Enum of this library and not to enums in general. I agree with you that this might not to be very clear.

Also note, this is true only for this Enum class where on the other side this library also provides a way to support serializing Enum instances in a limited way see https://github.com/marc-mabe/php-enum/tree/master?tab=readme-ov-file#serializing (that's why it must be done manually).

A PR would be welcome to clarify the doc block.

PS: Since PHP 8.1 enums are natively supported! As long as a EnumSet / EnumMap is not needed and no older PHP version targeted, I would highly recommend using native enums: https://www.php.net/manual/language.types.enumerations.php