From the course: PHP: Design Patterns

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Introducing the singleton pattern

Introducing the singleton pattern - PHP Tutorial

From the course: PHP: Design Patterns

Start my 1-month free trial

Introducing the singleton pattern

- The Singleton pattern, is kind of a problematic pattern. In fact, I debated with myself on whether to cover it in this course at all. This is because it's often overused, or completely misused. In fact, the rumor is that the Gang of Four design patterns book, won't include it in the next edition. That said, you will see Singletons in the wild, and there's some very limited scenarios where this is a good solution. So with that in mind, let's talk about what they are. Quite often, when we're building a system, there's some single resource that is shared by all components all over the system. A common one, is a database connection. If you open and close a database connection for each and every request on each and every page, it's easy to drain your connection pool and kill the database. Our first impulse, is to create a global object that gets shared by every class, function, method, whatever. While this solves one problem, it's still possible for there to be multiple instances of a…

Contents