Doğan Uçar
May 17, 2024

--

Hi Rob. PHP has the try method in each enum instance where a string is tried to convert to the backed enum. I would strongly recommend to use this method in order to work with enum instance.

I personally would also recommend not using backed enums too much and write instead a little helper that converts strings to enums. Avoiding backed enums and using pure enums, you do not rely too much on the string representation and make e.g refactorings easier.

Hope this answers your question 😊 if you are interested in more content like this, you can register to my newsletter by subscribing in the overlay: https://dogan-ucar.de/blog

--

--

Doğan Uçar
Doğan Uçar

Written by Doğan Uçar

Software Engineer, PHP/Laminas (Zend), Backend, Cloud, Freelancer & CEO, Open Source Contributor

No responses yet