How to create singleton design pattern in PHP 5 ? Last Updated : 06 Jul, 2021 Comments Improve Suggest changes Like Article Like Report When you don't want to have more than a single instance of a given class, then the Singleton Design Pattern is used and hence the name is - Singleton. Singleton is the design patterns in PHP OOPs concept that is a special kind of class that can be instantiated only once. If the object of that class is already instantiated then, instead of creating a new one, it gets returned. Normally when using various objects & classes, define the class only once and then create many objects and instances in our application with each object/instance having its own property. For example, when we are having a class name called "Student" with three attributes - "Fore_Name", "Middle_Name" and "Sur_Name". Each instance of that "Student" might or might not be having different values for "Fore_Name", "Middle_Name" and "Sur_Name". But if we use a singleton design pattern, there could never be more than a single instance of a given class ever in that given point of the program. The reason is quite simple. Suppose if we want our application to only ever have just one connection in a database, then we have to create a singleton class called "DataBase Connector" whose job is to ensure that there would be only a single DataBase connection in our program. Further meaning that we can access that particular instance quite globally so that we wouldn't have to get the database connection object between functions get passed so that it could be accessed from each and every place on earth.The major reason to use the Singleton Design Pattern is that we can use the Singleton Design Pattern object globally and unlike other normal classes, it could only contain one kind of object or one kind of instance. Sometimes, when there is an object that is created only once like the DataBase connection, then the use of Singleton is much more preferable. But note that the constructor method needs to be in private to make the class Singleton.Below program illustrates the Singleton Design Pattern:Example: php <?php class DataBaseConnector { private static $obj; private final function __construct() { echo __CLASS__ . " initialize only once "; } public static function getConnect() { if (!isset(self::$obj)) { self::$obj = new DataBaseConnector(); } return self::$obj; } } $obj1 = DataBaseConnector::getConnect(); $obj2 = DataBaseConnector::getConnect(); var_dump($obj1 == $obj2); ?> Output DataBaseConnector initialize only once bool(true) Comment More infoAdvertise with us Next Article How to create singleton design pattern in PHP 5 ? S SohomPramanick Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to create static classes in PHP ? A class is a user-defined data type that holds its own data members and member functions that can be accessed and used by creating one or more instances of that class. Every time a class is instantiated, the values that it holds are different and unique to the particular instance or object and not t 2 min read How to Define an Empty Object in PHP ? An object is an instance of a class, and we can create objects using various approaches. Defining an empty object typically involves creating an instance without specifying a class definition explicitly. In this article, we will explore different approaches to defining an empty object in PHP, these 1 min read Scope Resolution operator in PHP The scope resolution operator also known as Paamayim Nekudotayim or more commonly known as the double colon is a token that allows access to static, constant, and overridden properties or methods of a class. It is used to refer to blocks or codes in context to classes, objects, etc. An identifier is 2 min read When to use static vs instantiated classes in PHP? Prerequisite - Static Function PHP In PHP, we can have both static as well as non-static (instantiated) classes. Static class Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (m 5 min read How to mimic multiple constructors in PHP ? Constructors are special member functions for initial settings of newly created object instances from a class. In PHP, a constructor is a method named __construct(), which is called by the keyword new after creating the object. Constructors can also accept arguments, in which case, when the new stat 4 min read Like