For those who make a living by creating custom solutions for others using WordPress, you likely have your approach to how you structure the foundation of your typical plugin. And I think that’s a Good Thing™.
If you’ve read any of my previous posts, you know that I generally prefer the object-oriented approach to building plugins so there are a number of patterns, common solutions, and architectural decisions that I try to employ from project-to-project.
I’m not dogmatic about it, though:
If something new comes along, I’m willing to give it a try. Similarly, if someone I’ve been using for a while is criticized, I’m willing to take a look but that doesn’t mean I’ll change it.
In short, I need a lot of information to sell my on why I should change something that works, that’s maintainable, and that reads well for other developers.
And one of the points of discussion that comes up periodically is the idea of using Singletons in WordPress development.
Singletons in WordPress
I’ve talked about the design pattern (and others) in previous posts, and I’ve shared how to implement this particular pattern in your work.
Personally, I don’t consider it an antipattern. But there’s a caveat to that:
I don’t consider it an antipattern when it’s used under very specific circumstances and it’s not used as a way to circumvent better object-oriented principles.
When any of the above happens, we’ve turned it into an antipattern. Secondly, I don’t want to be a person who’s spreading any kind of message that it’s always okay to use singletons in WordPress.
It’s not. Please don’t do it.
But as I said earlier, it takes a fair amount of information to change my mind when it comes to changing something completely when I’m not totally sold on the idea there’s a fault with a given topic. And singletons in WordPress is one of those.
Gary Jones, however, a notable object-oriented expert and active WordPress community member maintain a gist of reasons, posts, and third-party resources as to why we should avoid using singletons.
You can star the gist here, but here are links to some of the articles that I think are worth reading:
- Google’s Clean Code Talks
- You Should Be Wary When Using The Singleton Pattern
- Singletons in PHP: Why They Are Bad
And there are many more resources.
Granted, it helps to understand the Singleton Pattern so I’m assuming that you do. The Wikipedia definition is sufficient:
In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
But perhaps an easier to way to explain it would:
A class that creates and returns a single instance of itself. Hence the name singleton.
Does it have its place? I think so. Is it overused? Yes. And that doesn’t mean I think we should avoid advanced strategies (like dependency injection) or anything like that.
But I also don’t think that every problem that we need to tackle has to be done in a way that’s overkill for the problem at hand.
So it’s important to be pragmatic.
So Singletons or Not?
Ultimately, the point of the post is two-fold:
- Educate yourself around what the singleton pattern is and determine how much you’re using singletons in WordPress,
- Determine if there are better, alternative ways to implement a solution.
In short, I’m not totally opposed to singletons in WordPress. At least not right now. But I do think there are far better patterns for more complex problems and it’s important to determine what works best when and where for a given problem.
That seems clear enough, right? 🙂
Leave a Reply
You must be logged in to post a comment.