Before I start a post talking about using singletons in WordPress (or, more appropriately, the Singleton Pattern), I want to make sure that you read the following two articles:
- Cloning in PHP, or why your singleton might kill WordPress by Thomas Scholz
- Singletons and their use in WordPress by Carl Alexander
Both of these articles provide immensely valuable perspective on this pattern and the dangers of using it whenever using it throughout our work in WordPress; however, does that mean we should completely avoid them?
I don’t think so.
Then again, I also recognize that the articles aren’t saying to completely avoid them. They are giving strong cases for how to use them and the dangers of using them should you opt to do so.
And though I’ve definitely used them in the past, I’ve generally stopped. However, I recently came across a use case for a project in which I think it’s acceptable.