PHP single-instance database implementation code sharing, php database _ PHP Tutorial

Source: Internet
Author: User
Code sharing for the single-instance mode of databases in PHP, php database. In PHP, the implementation code of the database Singleton mode is shared. First of all, we need to know the concept of Singleton mode. what is Singleton mode? The Singleton mode, as its name implies, shares the code for implementing the Singleton mode in PHP databases.

First, we need to know the concept of Singleton mode. what is 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.


Php static variables and single-instance mode of database classes

The maximum scope of a variable (non-Session) in php is a request. every request reinitializes the static variable.

Why use the PHP Singleton mode and application instance?

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 key points: one is that a class can only have one instance; the other is that it must create this instance; the third is that it must provide this 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.

First, we need to know the concept of Singleton mode. what is Singleton mode? The Singleton mode, as its name implies, is...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.