Enums do not support properties. This is due to the simple fact that enums are forbidden to have any form of state. But sometimes you need to store something.
For example, if you use Enumhancer
outside a framework with
dependency injection and you want to inject a
TransitionHook or a
Mapper.
Note: Just to follow the rules of PHP, the properties are stored per
enum class
or globally for all enums and the methods are therefore static
.
use Henzeb\Enumhancer\Concerns\Properties;
enum YourEnum: string {
use Properties;
// ..
}
YourEnum::property('your_property'); // will return null;
YourEnum::property('your_property', 'your_value');
YourEnum::property('your_property'); // will return 'your_value'
YourEnum::property('your_property', 100);
YourEnum::property('your_property'); // will return 100
YourEnum::property('your_property', null);
YourEnum::property('your_property'); // will return null
YourEnum::property('your_property', fn()=>'true');
YourEnum::property('your_property'); // will return callable
YourEnum::property('your_property', new stdClass());
YourEnum::property('your_property'); // will return stdClass() instance
YourEnum::unset('your_property'); // will remove your_property
YourEnum::unsetAll(); // will clear all properties.
You can also set global properties.
Henzeb\Enumhancer\Helpers\EnumProperties::global('property', 'your_value');
Henzeb\Enumhancer\Helpers\EnumProperties::clearGlobal(); // clear all global properties