Post #2046295
2025-09-17 08:03 UTC
Have been reminded by a fellow #PHP dev that, instead of a simple array, one could also use WeakMap...
Nice thing with this is that it is a real dictionary... and that it accepts objects as keys... and this includes `Enum::case`
https://www.php.net/manual/en/class.weakmap.php
Replies (5)
-
@dgoosens@phpc.social 2025-09-18 07:55
so apparently this fellow #php dev _is_ on #mastodon all credits to @gheb_dev@piaille.fr
-
@b_viguier@phpc.social 2025-09-17 09:52
@dgoosens@phpc.social it works because Enum are « persistent », isn’t it? Because WeakMap doesn’t prevent the object to be destroyed. Wouldn’t SplObjectStorage be a safer choice ? Do you see other advantages in WeakMap for your use case? 🤔 Anyway, thanks for sharing 🙂
-
@Exakat@phpc.social 2025-09-17 18:57
@dgoosens@phpc.social Nice. This goes into the #phptip archive! 😀 :elephpant:
-
@dseguy@phpc.social 2025-09-17 18:59
@dgoosens@phpc.social Hold on a minut... Did you do a weekmap, as an illustration of your weakmap? Nice!
-
@Windy@phpc.social 2025-09-18 09:10
@dgoosens@phpc.social This is interesting to learn. Thank you for sharing it.