Multi-Instance Factories in PHP
·
I advocate the proper use of Singletons, but that situations where Singletons are appropriate are few and far between.
·
I advocate the proper use of Singletons, but that situations where Singletons are appropriate are few and far between.
·
When I ask developers, “why’d you use a Singleton here,” more often than not they reference a tutorial, another developer, or “that’s how I’ve always done it.” No one ever takes the time to learn about the patterns (or anti-patterns) they’re using so they can make informed decisions about software design.
·
There are valid reasons for using a Singleton in your design – these reasons, however, are rare enough that many developers can avoid them entirely and still have a full career. If you find yourself using Singletons often, you’re doing something wrong.
·
Last time, I argued in favor of the Singleton pattern in WordPress. Singletons make sense in WordPress specifically for several reasons: They live in the global scope without using the already abused/overused [cci]global[/cci] keyword As a distributed application maintained by several hundred developers, they prevent problems that likely arise from others misusing your code But
·
Please also review my follow-up post, Making Singletons Safe in PHP. There has been a lot of discussion this week regarding the Singleton pattern and whether or not it has a place in modern software development. Mike Schinkel wrote a tutorial about a Singleton factory. I wrote one about using Singletons as a WordPress plugin
·
The power of WordPress lies in its pluggable infrastructure. Unlike other content management applications which require you to edit the core code of the system to make changes, WordPress supports plugins. These smaller applications tie in to a rich API provided by WordPress that allows you to change just about everything about the application and customize