Back to Top

How To use Singleton Design Pattern in PHP

Singleton Pattern in PHP

Design Pattern performs the major role in the object-oriented programming of PHP.A design pattern is useful to write a real and reliable design code and organize code in the specific design.A design pattern is important helpful for other programmers to immediately understand your code, without in-depth study or any dependency on you.You might wonder why you should care, if class have multiple instances.isn’t it? So,Let’s understand the important of Singleton Design Pattern in PHP and clear your query.

What is Singleton Design?

The Singleton Pattern is one of the best and well-known design patterns.A singleton Pattern in one kind of design pattern and it comes when you want single instance for whole application(means you want application wide instance).Singleton pattern is to keep only one instance of an object and that instance will work as the global access point for the class.

Specifically, for a login object, you would want each and every place in the application that wants to print something to the log that user have access to it and so to centralize the logging mechanism and to handle the filtering of messages according to log level settings we can use Singleton patterns.

To Make a class as a singleton class in usually done with implementing a static class method.the static method returns the only single instance of the class.When a first time you call this method, it creates an instance, saves it in a private static variable and returns the instance. The next time, it just returns you a handle to the already created instance.

Example:

Let’s have a look with Singleton Pattern

Singleton Pattern requires a static member variable that holds a unique instance of the class so we have checked if self::$instance is not defined assign the class object to it in getInstance method.

The above example describes the implementation of Singleton Design Pattern and here I am only creating one instance of the database object and only that can be connected to a database rather than executing multiple instances which is very important to save memory and improve efficiency of code.Pattern User::getInstance() gives you access to the logging object from anywhere in your application, whether it is from a function, a method, or the global scope.

Remember constructor must be declared as private for Singleton Pattern so the external code can not access the class methods which might affect the Singleton Pattern.Read about Cloning Object in PHP so you get an idea how to copy the instance of the object which also affect the Singleton Pattern.

In above example, the constructor and clone methods are defined as private.

Advantages:

1. You can avoid a lot of new operating resources consumption using the Singleton pattern.

Disadvantages:

1 As the constructor of Singleton Pattern private, Singleton can not be inherited.

2. You can not clone or copy the instance of the class using Singleton.

This is done so that a developer can’t create a second instance of the User class using the new or clone operators; therefore, getInstance() is the only way to access the singleton class instance.

Hope this article will helpful to understand the Singleton Pattern.If you have any query or confusion, let me know via comment.

Comments (1)

  1. Nice article. Just an FYI, using PHP5.4 traits, singletons become much easier to scale across any number of classes you need.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How To Store Binary Data in PHP

Posted on 6 years ago

Bhumi

How to use Flickr API with PHP

Posted on 6 years ago

Bhumi