Skip to content

Commit d56c200

Browse files
authored
Merge pull request #158 from drealecs/native_enum_migration_documentation
document migration to native enums
2 parents 8bb24a0 + f3f9aa6 commit d56c200

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,56 @@ final class Action extends Enum
130130
}
131131
```
132132

133+
## Native enums and migration
134+
Native enum arrived to PHP in version 8.1: https://www.php.net/enumerations
135+
If your project is running PHP 8.1+ or your library has it as a minimum requirement you should use it instead of this library.
136+
137+
When migrating from `myclabs/php-enum`, the effort should be small if the usage was in the recommended way:
138+
- private constants
139+
- final classes
140+
- no method overridden
141+
142+
Changes for migration:
143+
- Class definition should be changed from
144+
```php
145+
/**
146+
* @method static Action VIEW()
147+
* @method static Action EDIT()
148+
*/
149+
final class Action extends Enum
150+
{
151+
private const VIEW = 'view';
152+
private const EDIT = 'edit';
153+
}
154+
```
155+
to
156+
```php
157+
enum Action: string
158+
{
159+
case VIEW = 'view';
160+
case EDIT = 'edit';
161+
}
162+
```
163+
All places where the class was used as a type will continue to work.
164+
165+
Usages and the change needed:
166+
167+
| Operation | myclabs/php-enum | native enum |
168+
|----------------------------------------------------------------|----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
169+
| Obtain an instance will change from | `$enumCase = Action::VIEW()` | `$enumCase = Action::VIEW` |
170+
| Create an enum from a backed value | `$enumCase = new Action('view')` | `$enumCase = Action::from('view')` |
171+
| Get the backed value of the enum instance | `$enumCase->getValue()` | `$enumCase->value` |
172+
| Compare two enum instances | `$enumCase1 == $enumCase2` <br/> or <br/> `$enumCase1->equals($enumCase2)` | `$enumCase1 === $enumCase2` |
173+
| Get the key/name of the enum instance | `$enumCase->getKey()` | `$enumCase->name` |
174+
| Get a list of all the possible instances of the enum | `Action::values()` | `Action::cases()` |
175+
| Get a map of possible instances of the enum mapped by name | `Action::values()` | `array_combine(array_map(fn($case) => $case->name, Action::cases()), Action::cases())` <br/> or <br/> `(new ReflectionEnum(Action::class))->getConstants()` |
176+
| Get a list of all possible names of the enum | `Action::keys()` | `array_map(fn($case) => $case->name, Action::cases())` |
177+
| Get a list of all possible backed values of the enum | `Action::toArray()` | `array_map(fn($case) => $case->value, Action::cases())` |
178+
| Get a map of possible backed values of the enum mapped by name | `Action::toArray()` | `array_combine(array_map(fn($case) => $case->name, Action::cases()), array_map(fn($case) => $case->value, Action::cases()))` <br/> or <br/> `array_map(fn($case) => $case->value, (new ReflectionEnum(Action::class))->getConstants()))` |
179+
133180
## Related projects
134181

182+
- [PHP 8.1+ native enum](https://www.php.net/enumerations)
135183
- [Doctrine enum mapping](https://github.com/acelaya/doctrine-enum-type)
136184
- [Symfony ParamConverter integration](https://github.com/Ex3v/MyCLabsEnumParamConverter)
137185
- [PHPStan integration](https://github.com/timeweb/phpstan-enum)

0 commit comments

Comments
 (0)