Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92662ad

Browse files
committed
document how migration to native php enums will work
1 parent b942d26 commit 92662ad

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎README.md

Lines changed: 48 additions & 0 deletions
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 have 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 class without extending the enum class
140+
- no static 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)

AltStyle によって変換されたページ (->オリジナル) /