Single-sample mode for PHP (Singleton Pattern single-piece mode or single-element mode)
The Singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system.
The Singleton mode is a common design mode. in computer systems, A thread pool, cache, log object, dialog box, printer, database operation, and graphics card driver is often designed as a singleton.
There are three Singleton modes: lazy singleton, hungry Singleton, and registered Singleton.
The Singleton mode has the following three features:
1. only one instance is allowed.
2. you must create this instance on your own.
3. this instance must be provided to other objects.
So why use the PHP Singleton mode?
One of the main application scenarios of PHP is the scenario where applications interact with databases. in one application, there will be a lot of database operations, and the database handles the connection to the database, the Singleton mode can avoid a large number of new operations. Because each new operation consumes system and memory resources.
In previous project development, before the Singleton mode was used:
// Initialize a database handle $ db = new DB (...); // for example, an application scenario is to add a user information $ db-> addUserInfo ();...... // However, we need to use this user information in another place. in this case, we need to use the database handle resource. This may happen ...... function test () {$ db = new DB (...); $ db-> getUserInfo ();...... some may say that you can directly use the global keyword! Global $ db ;......
It is true that global can solve the problem and also play the role of Singleton mode, but in OOP, we reject this encoding. Because global has security risks (the nature of global variables is unprotected ).
Global variables are one of the main causes of bugs for object-oriented programmers. This is because global variables bind classes to a specific environment and destroy encapsulation. If the new application cannot guarantee that the same global variables are defined at the beginning, A class dependent on global variables cannot be extracted from an application and applied to a new application.
Specifically, the singleton mode is just an improvement on global variables to prevent global variables that store unique instances from polluting the namespace. You cannot overwrite a singleton with data of the error type. This protection is especially important in PHP versions that do not support namespaces. Because naming conflicts in PHP are captured during compilation and the script stops running.
PHP Singleton mode instance:
First look at the figure:
In the above Object diagram, there is a "singleton object", and "customer a", "Customer B", and "customer C" are the three customer objects of the singleton object. As you can see, all customer objects share one singleton object. In addition, we can see from the Singleton object to its own connection line that the singleton object holds a reference to itself.
Advantages and disadvantages of Singleton mode:
Advantages:
1. improved system design
2. it is an improvement on global variables.
Disadvantages:
1. difficult to debug
2. hidden dependency
3. unable to overwrite a singleton with data of the error type