© Kelt Dockins 2017

Kelt Dockins, Design Patterns in PHP and Laravel, 10.1007/978-1-4842-2451-9_7

7. Singleton

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout singleton

Intent

Ensure a class only has one instance and provide a global point of access to it.1

Applications

Short answer: none. Honestly, I have yet to find any practical applications for the singleton pattern in PHP. The original singleton design pattern was made to ensure that only one instance of a class could ever be created. It was used in the context of printer spoolers where you would only ever want one instance created. Out of the box, PHP is a single-threaded environment and usually has a short lifetime within an apache/nginx thread. Even if you used something like React ...

Get Design Patterns in PHP and Laravel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.