Main Content

Singleton design pattern - examples and uses

Archive - Originally posted on "The Horse's Mouth" - 2016-01-20 16:43:04 - Graham Ellis

A Singleton class is one where you can only create one object of a type, or a second object is really a share of the first. An example in PHP of a Singleton design pattern - [here]. Uses a static (class) variable to flag object creation.

A singleton is especially sueful when you're looking to hold a quasi-global set of information or a shared database within an application, without wanting to actually have a global variable with all the namespace clutter that can bring. A further example from today's Object Oriented PHP course can be found [here].

Singletons are great for such tasks as database connections, loading reference tables, etc ...