kboyd,
@kboyd@phpc.social avatar

converts a hobby project to use php enums

🤩🤩🤩

alessandrolai,
@alessandrolai@phpc.social avatar

@kboyd right?? Refactoring toward enums is so so satisfying!

kboyd,
@kboyd@phpc.social avatar

@alessandrolai I was able to consolidate a bunch of user status variables/constants/strings together into a single source of truth. So nice.

Even worked alright in twig, where I could compare to constant('\Path\To\Enum::Value')

cabbey,
@cabbey@phpc.social avatar

@kboyd @alessandrolai I have a couple in a feature dev branch at work. Gotta admit they’re pretty damn nice to code with, way better than const ints. Only gripe I have so far is that I can’t put them into log messages or context or even simple echos without doing some kind of special one off weirdo function on it and calling that. One of the few places I want a $@&*^£ magic __toString() but we can’t have it. :(

kboyd,
@kboyd@phpc.social avatar

@cabbey @alessandrolai You mean ->name and ->value?

kboyd,
@kboyd@phpc.social avatar

@cabbey ooh, you can check "instanceof BackedEnum", so monolog could be updated to grab the ->value and ->name in that case (if it hasn't already)

cabbey,
@cabbey@phpc.social avatar

@kboyd sounds like a context processor. Could also handle casting bools as ‘T’ or ‘F’ instead of ‘1’ and ‘’.

alessandrolai,
@alessandrolai@phpc.social avatar

@cabbey @kboyd can't you manually implement \Stringable on them?

cabbey,
@cabbey@phpc.social avatar
alessandrolai,
@alessandrolai@phpc.social avatar

@cabbey @kboyd aw crap, it's covered in the original RFC:

"However, implicit magic method calls such as ArrayAccess on enums are not allowed in static or constant definitions as we cannot absolutely guarantee that the resulting value is deterministic or that the method invocation is free of side effects. Function calls, method calls, and property access continue to be invalid operations in constant expressions."

https://wiki.php.net/rfc/enumerations

cabbey,
@cabbey@phpc.social avatar

@alessandrolai @kboyd yeah. I just think that restriction was implemented a little too tightly. :( non static or const contexts got the short end of that deal imho, but I dunno what kind of gymnastics the internals would require to allow what I want. And I worked around it with a custom instance method that I needed for other purposes anyway. 🤷

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • kavyap
  • thenastyranch
  • cubers
  • ethstaker
  • InstantRegret
  • DreamBathrooms
  • ngwrru68w68
  • magazineikmin
  • everett
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • GTA5RPClips
  • JUstTest
  • khanakhh
  • normalnudes
  • osvaldo12
  • cisconetworking
  • provamag3
  • Durango
  • tacticalgear
  • modclub
  • Leos
  • megavids
  • tester
  • anitta
  • lostlight
  • All magazines