Hướng dẫn php enum naming convention
Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today. Show This is my personal style guide for using enums in PHP. Each section describes the rule, but also the personal, opinionated reasoning behind the rule. # 1. UppercaseYou must use uppercase notation for enum cases:
# Why?Enums are very close to "constant values". Even though the RFC showed them using PascalCase (inspired by other languages), it doesn't give a good reason for doing so. Some people argue to use PascalCase for enums specifically to visually distinguish between constants and enums. I don't believe that relying on naming conventions to distinguish between concepts is a good idea. It's better to rely on your IDE's or editor's insights to know what something is, instead of holding onto a naming convention. Ironically, uppercase constants exist because of the same reason: to distinguish them from normal variables. I want to acknowledge that fact, however there's little we can do to change decades of programming history. I think it's fair to say that the majority of PHP projects use uppercase constants, and so I would like to keep following that convention for closely related concepts, despite how the convention originated. Nevertheless, I have tried using PascalCase for enums, but it always felt unnatural to me: I wrote my enum cases uppercase automatically, only to notice I did afterwards; meaning I had to go back and refactor them. I decided not to fight my instincts and just go with uppercase notation which feels the most natural to me, because of how constant values have been written for years, and how close enums are to constants anyway. # 2. Don't overuse backed enumsYou should only use backed enums when you actually have a need for them. A common use case is serialization. If an enum is only going to be used within code, you shouldn't make it a backed enum.
# Why?Manually assigning values to enums introduces maintenance overhead: you're not only maintaining the enum cases itself, but also their values. It's important to take into consideration when to add overhead, and when not. On top of that: enums have their built-in
When in doubt, apply this rule: do you need to create this enum from scalar values (from a database, from user input, …): use a backed enum. If the answer is "no" however, hold off assigning values until your use case requires it. # 3. Simple match methods are allowedEnums shouldn't contain complex methods. Ideally, they only contain methods using the
# Why?Enums shouldn't provide complex functionality, if you find yourself writing too much logic in your enums, you probably want to look into using the state pattern instead. A good rule of thumb is that if it can fit into a # 4. Values shouldn't be used as labelsA backed enum's value should only be used as a value for (de)serialization, not for functionality like providing labels. # Why?You might be tempted to rewrite the example from the previous point as follows:
While shorter, you're now mixing two concerns into one: representing labels in a user-readable format, combined with their serialization value. The first one is very likely to change over time, the second one should stay unchanged as much as possible to prevent complex data migrations. # 5. No index valuesYou should only use integer backed enums when their values actually are integers, not to assign some notion of "order" or "indexing" to your enums.
# Why?It might be tempting to assign integer values to, for example, status enums where While such sequential indexing works in a limited amount of cases, it's a mess to maintain. What happens when you're required to add another state called
When in doubt, you can again use the state pattern to model complex transitions between states and their order instead. Do you agree? Disagree? Let me know on Twitter or via email! Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter: |