This article mainly introduces the implementation code sharing of the database Singleton mode in PHP. This article first explains some knowledge about Singleton mode and then provides the implementation code of the database Singleton mode, for more information about the singleton mode, see the singleton mode?
The Singleton mode, as its name implies, has only one instance.
As the object creation mode, the singleton mode ensures that a class has only one instance, and it is instantiated and provided to the entire system. This class is called the singleton class.
The Singleton mode has three main points:
First, a class can only have one instance;
Second, it must create the instance on its own;
Third, it must provide the instance to the entire system.
Next we will discuss why we should use the PHP Singleton mode?
Most people understand the purpose of the singleton model literally. They think that this is a saving of system resources and can avoid repeated instantiation. It is a kind of "family planning ". PHP clears all resources from the memory after each execution of the page. therefore, in PHP, the singleton needs to be re-instantiated every time it is run, thus losing the significance of the singleton repeated instantiation. in this regard, the PHP Singleton is indeed a bit disappointing. but does the singleton only have this function and application? The answer is no. Let's take a look.
1. php applications mainly involve database applications. Therefore, a large number of database operations exist in an application. When using the object-oriented method for Development (nonsense), if you use the singleton mode, this avoids the resource consumption of a large number of new operations.
2. If you need a class in the system to globally control some configuration information, you can easily implement it using the singleton mode. For details, refer to the FrontController section of zend Framework.
3. in a page request, debugging is easy, because all the code (such as database operation db) is concentrated in one class, we can set hooks in the class and output logs, this avoids var_dump and echo everywhere.
class db { public static $cennct = null; private function __construct(){return false;} private function conn(){ $pdo = new PDO('mysql:host=localhost;dbname=dbname','root',''); $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); $pdo->query('set names utf8'); return $pdo; } public static function getdb(){ if(self::$cennct == null ) self::$cennct = self::conn(); return true; } protected function fetch($sql,$param=array()){ $this->getdb(); $tmp = self::$cennct->prepare($sql); $tmp->execute($param); return $tmp->fetch(PDO::FETCH_ASSOC); } protected function fetchAll($sql,$param=array()){ $this->getdb(); $tmp = self::$cennct->prepare($sql); $tmp->execute($param); return $tmp->fetchAll(PDO::FETCH_ASSOC); } protected function execute($sql,$param=array()){ $this->getdb(); $tmp = self::$cennct->prepare($sql); return $tmp->execute($param); }}
The preceding is a database operation Singleton mode.