Experience Level: Intermediate to Advanced PHP
In the post – published July 17, 2017 – Alain Schlesser examines use of the Singleton pattern in WordPress development with PHP, whether it’s a good or bad idea, and alternative approaches to solve the same problems.
- What is a Singleton?
- Problems with the Singleton pattern: Singleton vs SOLID
- Singleton in practical usage
- Alternatives to using the Singleton pattern with code examples
- In-depth examination of the Factory and Static Proxy patterns, also service locators with code examples
- Using the WordPress Plugins API (as a Singleton alternative), with code examples
- Using some combination of the aforementioned patterns together
Although this post was published more than a year ago, the fact that it covers software design means that its usefulness doesn’t really have an expiration date. This post is something that the WordPress development community needs more of: in-depth examination of different common software patterns that can be used to solve specific problems.
Easily one of the biggest issues in WordPress plugin and theme development today is developers having very little sense of structure or forethought in how they structure their code. Just because the shoe fits now, doesn’t mean you should wear it, and it doesn’t mean it will always fit.
Even if you don’t end up implementing any of the suggested patterns from this post, this post is an excellent, authoritative example for a better approach to software design you come back to again and again and always glean something new.