The Case for Singletons in WordPress


Still, Singletons are considered an “anti-pattern” by many developers.  They claim it’s an overused pattern that’s misused by too many developers.  True, it can be misused, but so can Axe body spray and words like YOLO.  Overuse doesn’t make them wrong per se, you just need to be very judicious in when, where, why, and how you use them.

Wrapping my head around Singletons was a huge barrier to entry for me and OOP. It seemed like a four letter word and that if anything ended up being a Singleton you should feel bad. The majority of my work since I started out has been with WordPress. I, like many people in my situation, started writing (really terrible) procedural spaghetti. I learned of objects and things changed, but I was intimidated. I wasn’t able to turn my plugin workflow into car parts, lamp parts, or things so I often ended up with a Singleton without even knowing it.

Now I’m doing it on purpose from time to time. I still value OOP and am still trying to learn better patterns for my implementations, but I find that many times given the tasks I have (specifically in WordPress-based projects) a Singleton makes sense.