Singletons in PHP

If you’ve heard of design patterns you’ll have probably heard of the singleton pattern. It’s debatably the easiest to start with and get you on the road to becoming the Walter White of design patterns. Below I will explain what a singleton is, when you should use a singleton, rules that must be followed, drawbacks and I will provide sample code. Lets get started.

What is a singleton

As the name sort of implies, a singleton is a class that can only be initialised once per request to an application. E.g once per HTTP request on a website.

Continue reading