How to implement Singleton (Singleton mode) Singleton mode in PHP has the following features :? A singleton class can have only one instance. The singleton class must create its own unique instance. The singleton class must provide this instance to all other objects. Code: Singleton. php: & lt ;? PhpclassSingleton {??? Privatestatic $ instance; how to implement Singleton in PHP (Singleton mode)
The Singleton mode has the following features:
?
- A singleton class can have only one instance.
- The singleton class must create its own unique instance.
- The singleton class must provide this instance to all other objects.
Code:
Singleton. php:
Class Singleton
{
??? Private static $ instance;
??? Private function _ construct ()
??? {
??? }
??? Public static function getInstance ()
??? {
??????? If (self: $ instance = null)
??????? {
??????????? Self: $ instance = new Singleton ();
??????? }
??????? Return self: $ instance;
??? }
}
?>
?
In use, because the constructor is private (private), it cannot be instantiated directly. you must use a method similar to the following:
Example:
Require_once ('Singleton. php ');
$ Instance = Singleton: getInstance ();
?>