Home >Backend Development >PHP Tutorial >Detailed explanation of singleton pattern in PHP implementation design pattern, detailed explanation of PHP design pattern_PHP tutorial
【Summary】
Ensure that a class has only one instance and provide a global access point to access it [GOF95]
【Features】
1. A class has only one instance
2. It must create this instance by itself
3. You must provide this instance to the entire system yourself
【Structural Diagram】
【Main Character】
Singleton defines an Instance operation, allowing clients to access its only instance. Instance is a class method. Responsible for creating its only instance.
【Advantages and Disadvantages】
1. Controlled access to unique instance
2. Reduce the namespace. The singleton mode is an improvement on global variables. It avoids polluting the namespace with global variables that store unique instances
3. Allows for the essence of operation and presentation. Singleton classes can have subclasses. And it is very easy to configure an application with an instance of this extended class. You can configure your application at runtime with instances of the classes you need.
4. Allow a variable number of instances (multiple instance mode)
5. More flexible than class operations
【Applicability】
1. When a class can only have one instance and clients can access it from a well-known access point
2. When this only instance should be extensible through subclassing. And users should be able to use an extended instance without changing their code.
[Singleton mode php instance]
$s = Singleton::getInstance();
echo $s->info();
?>
Example #1 Call factory method (with parameters)
class Example
{
// The parameterized factory method
public static function factory($ type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
?>
------------ --------------------------
Example #2 Singleton pattern
class Example
{
// Save the class instance in this attribute
private static $instance;
// The constructor is declared private to prevent direct creation of objects
private function __construct()
{
echo 'I am constructed';
}
// singleton method
public static function singleton()
{
if (!isset(self:: $instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
// Normal method in Example class
public function bark()
{
echo 'Woof!';
}
// Prevent users from copying object instances
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
?>
When it comes to Java, programmers say it is a singleton design pattern. In PHP, it is usually called a singleton pattern. The term is different. It is also introduced in the manual:
Singleton is used to generate a class A unique object. The most commonly used place is database connection. After using the singleton pattern to generate an object, the object can be used by many other objects.
class Example
{
// Save the class instance in this attribute
private static $instance;
// The constructor is declared private, Prevent direct creation of objects
private function __construct()
{
echo 'I am constructed';
}
// singleton method
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
// Ordinary method in Example class
public function bark()
{
echo 'Woof!';
}
// Prevent users from copying object instances
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
?>
In this way we can get a unique object of the Example class.
// This way of writing will go wrong because the constructor is declared as private
$test = new Example;
// The following will get the Example class Singleton object
$test = Example::singleton();
$test->bark();
// Copying the object will result in an E_USER_ERROR.
$test_clone = clone $test;
?>