BenSampo / laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.
https://sampo.co.uk/blog/using-enums-in-laravel
MIT License
2k stars 164 forks source link

Cache Enum value instances to allow strict comparison #178

Closed hackel closed 3 years ago

hackel commented 4 years ago

Several other PHP Enum libraries implement the multiton pattern so that only a single instance of a particular Enum value is ever returned, thereby allowing strict comparisons. Is this a feature you would be interested in adding to this library?

$admin = UserType::fromValue(UserType::Administrator);
// Instead of:
$admin->is(UserType::Administrator()); // true
// One could also check equality directly:
$admin === UserType::Administrator(); // true

The implementation would be quite simple, and could even use WeakReferences if the requirements were increased to PHP 7.4.

(See https://github.com/Elao/PhpEnums#compare for an example.)

BenSampo commented 4 years ago

Seems like a good idea to me @hackel, nice suggestion. Do you want to submit a PR?

BenSampo commented 3 years ago

Given that Enums will be a core part of PHP soon, this probably isn't worth implementing any more.