Skip to Content
Mastering PHP 7
book

Mastering PHP 7

by Branko Ajzele
June 2017
Intermediate to advanced
536 pages
9h 49m
English
Packt Publishing
Content preview from Mastering PHP 7

The singleton pattern

The singleton is among the first design patterns most developers learn. The goal of this design pattern is to limit the number of class instantiations to only one. What this means is that using the new keyword on a class will always return one and the same object instance. This is a powerful concept that allows us to implement all sorts of application-wide objects, such as loggers, mailers, registries, and other bits of functionality that we may want to act as singletons. However, as we will soon see, we will avoid the new keyword altogether, and instantiate an object via the static class method.

The following example demonstrates a possible singleton pattern implementation:

<?phpclass Logger{    private static $instance ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning PHP 7

Learning PHP 7

Antonio L Zapata (GBP)
Upgrading to PHP 5

Upgrading to PHP 5

Adam Trachtenberg

Publisher Resources

ISBN: 9781785882814Supplemental Content