What is Singleton Design Pattern in PHP?
It is basically used to restrict the class instantiation to a single object. It may be useful in those cases if only one object needs throughout the system. They are designed to ensure there is s single instance of a class which is the global point of access to it. We have also lazy initialization as well as global access along with single instance.
Objective – The main objective of singleton design pattern is to ensure that it has only one class instance as well as provide a global point of access to retrieve it.
Things to Know About Singleton Pattern
- This singleton design pattern forms part from one of the available patterns originating from aka GoF.
- The singleton design pattern is in itself is responsibility pattern implementation
- The use of one object is advocated by the singleton design pattern which is solely responsible for doing one particular task. For example – Zend uses the single object class instance so it is good to communication in databases.
- Within an application or system, the singleton design pattern is used to create only one single point of entry as well as functionality.
Usage of Singleton Pattern
- While creating a layer of the database you would intend to use only one instance of class for communication with the database. This would help in maintaining solely one connection instead of using multiple ones which might be otherwise consumption of the resource.
- The singleton design pattern assists in maintaining the synchronized state of the system if you have a big or complex system.
- If you observe that many of the methods which you are accessing within the projects come from one recurring object. Then this is an aspirant for being a singleton.
Singleton Pattern Anatomy in PHP Context
The following conditions which you need to fulfill
- Static Member Variable Presence – act as a placeholder for the class instance.
- By locking down the constructor of that class – just make the visibility private.
- Make it private to prevent any instance or object of that class to be cloned which is to prevent any entity within the system to make copy of this object
- A public static method – for accessing the class instance have a single globally accessible static method
Cons of Singleton Design Pattern
- Even singleton design pattern sounding very sexy but there are some flaws such as performance issue so utilize it sparingly. For example – owing many singletons Zend 1.x suffer a slight performance issue.
- Since it tests individual objects so might be an issue for unit testing.
- Might hinder flexibility within your classes if not carefully planned.
Singleton as Anti-Pattern
By many developers, singleton design pattern is considered as Anti-pattern. Now question arise what basically Anti-pattern is? – They are basically design solutions which are usually ineffective as well as present a high risk of being counterproductive.
In spite of many advantages, it has some flaws but there is some uniqueness which makes singleton design pattern apart from others.