Learn for yourself only
PHP Single-instance mode
Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system. That is, a class has only one instance of an object.
The main points are three:
1. A class can have only one object;
2. The object of this class must be created on its own;
3, to the whole system to provide this one object;
From the specific implementation point of view, is the following three points:
One is that the Singleton mode class only provides a private constructor,
The second is that the class definition contains a static private object of the class.
The third is that the class provides a static public function to create or get a static private object of its own.
Of course, there must be a private clone method to prevent cloning;
Advantage one, the instance control singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique instances. Ii. flexibility because classes control the instantiation process, classes can flexibly change the instantiation process. Disadvantage one, although the amount of overhead is very small, it will still require some overhead if you want to check if an instance of the class exists every time the object requests a reference. This problem can be resolved by using static initialization. Ii. possible development confusion when using singleton objects, especially those defined in a class library, developers must remember that they cannot instantiate an object using the New keyword. Because library source code may not be accessible, application developers may unexpectedly find themselves unable to instantiate this class directly. Third, the object lifetime cannot resolve the problem of deleting a single object. In a language that provides memory management, such as a. NET framework-based language, only a singleton class can cause an instance to be deallocated because it contains a private reference to the instance. In some languages, such as C + +, other classes can delete object instances, but this results in a floating reference in a singleton class. (Here is not quite understand, solve) PHP single-Sample code demo:
1 <?php 2//Singleton 3 class uni{4 //Create a static private variable save the class object 5 static private $instance; 6 //Parameter 7 private $CONFI G 8 //Prevent direct creation of object 9 Private function __construct ($config) { $this-config = $config; echo "I was instantiated"; }13 //Prevent cloning objects in the private function __clone () { }17 static public function getinstance ($ Config) {/ //Determine if $instance is a Uni object ///No then create the if (!self:: $instance instanceof Self) {+ self :: $instance = new Self ($config), }23 return self:: $instance,}26 public function GetName () { echo $this, config;28 }29}30 $db 1 = uni::getinstance (1); $db 1, GetName (); echo "< br> $db 2 = uni::getinstance (4); $db 2, GetName ();?>
Operation Result:
I was instantiated 1.
1
$db 1 After creating the object, the parameter is 1
$db 2 does not create an object, but instead uses the unique object saved directly by $instance, so the output parameter does not change to 4;
Original link: https://www.cnblogs.com/wwjchina/p/7724271.html
PHP design mode--Singleton mode