Member-only story
Using PHP 8.1 enumerations in Symfony
Symfony 5.4/6.0 is supporting them since day one

I’ve been involved in the addition of enumerations support in the Symfony framework. Why? Because I’m mad at love with this new feature. I waited them for years in PHP. And I also love to contribute to the framework, so why not blend these two pleasures?
A news has been posted on Symfony blog about enumerations in the framework. You may read it before this article! This article is just a little supplement with additional code snippets and information.
If you never heard of PHP enumerations, here is a code snippet to show them in action:

Pretty nit. You’re able to define typed values without having to bother to their real value. Those are called Unit Enums. You’re also able to define a value for each enum case. By doing this, they’re called Backed Enums.
You’re able to use this in Symfony with ease. Without wondering if it will work “because the feature is so new”. Let’s see this together!
Forms
Alexander M. Turek (known as @derrabus on Twitter, Symfony Core Team member) contributed the new EnumType which allows you to create an form entry limited to an enumeration values. Here is how you’ll use it:

By doing this, you’re limiting user to the enumeration possible values. It is also way simpler to validate it!
About the HTML representation, it will simply display a select input. You can also pass the expanded
option to the form. By doing so, the input will now show as radio buttons. And as the parent type of this new EnumType is a ChoiceType, you can also use options like multiple
.
Find the complete pull request by Alexander M. Turek here.
Serializer
The Serializer component of Symfony is amazing when it comes to convert your objects and variables to formats like…