When PHP is running, since each HTTP request corresponds to a database PDO connection object instance, is the singleton mode meaningful in PHP? When PHP is running, since each HTTP request corresponds to a database PDO connection object instance, is the singleton mode meaningful in PHP?
Reply content:
When PHP is running, since each HTTP request corresponds to a database PDO connection object instance, is the singleton mode meaningful in PHP?
PDO connection objects are different in each request, but note that the difference here is the PHP Object, not the actual database connection. In Persistent mode, although a new PDO object is created every time a request is made, these objects actually have a pdo extension that controls the underlying connection of the shared database.
The Singleton mode is of course very useful. In Singleton mode, you only need to build a PHP Object, not to mention whether the PDO in these objects has the underlying shared implementation, the optical Singleton can save the memory and other expenses of the object.
In addition, there are two concepts. You can't put it together. Some underlying implementations of PHP are not subject to the lifecycle control of PHP requests. You cannot consider the implementation of pdo extensions in the PHP request cycle.