PHP Database Singleton mode implementation code sharing, PHP _php tutorial

Source: Internet
Author: User
Tags php database zend framework

PHP Database Singleton mode implementation code sharing, PHP databases


First, we need to know the concept of a singleton pattern, so what is a singleton pattern?

The singleton pattern, as its name implies, is only one instance.

As an object's creation pattern, a singleton pattern ensures that a class has only one instance, and instantiates it itself and provides it to the entire system, which we call a singleton class.

The main points of the singleton pattern are three:

One is that a class can have only one instance;
The second is that it must create this instance on its own;
Thirdly, it must provide this instance to the whole system on its own.

Let's discuss why we use PHP singleton mode.

Most people understand its purpose from the literal meaning of the singleton pattern, and think that it is a kind of "family planning" that saves the system resources and avoids duplication of instantiation. Each time PHP executes the page, it cleans up all the resources from memory. Thus, in PHP, the single instance of the actual operation needs to be re-instantiated, thus losing the meaning of a single case of repeated instantiation. In this respect alone, PHP's singleton is indeed a bit disappointing for you. But does the singleton only have this function and application? The answer is no, let's take a look.

1. PHP is mainly used in database applications, so there will be a large number of database operations in an application, in the use of object-oriented development (nonsense), if you use the singleton mode, you can avoid a large number of new operations to consume resources.

2. If a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode. This can be see the Frontcontroller section of the Zend Framework.

3. In a page request, it is easy to debug, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo.

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 above is a single database operation Singleton mode


PHP static variables and the singleton mode of the database class

The maximum scope of a variable (non-session) in PHP is one request, and the static variable is reinitialized for each request.

Why use PHP singleton mode and application example

The singleton pattern, as its name implies, is only one instance. As an object's creation pattern, a singleton pattern ensures that a class has only one instance, and instantiates it itself and provides it to the entire system, which we call a singleton class. The main points of the singleton pattern are three: one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own. Let's discuss why we use PHP singleton mode. Most people understand its purpose from the literal meaning of the singleton pattern, and think that it is a kind of "family planning" that saves the system resources and avoids duplication of instantiation. Each time PHP executes the page, it cleans up all the resources from memory. Thus, in PHP, the single instance of the actual operation needs to be re-instantiated, thus losing the meaning of a single case of repeated instantiation. In this respect alone, PHP's singleton is indeed a bit disappointing for you. But does the singleton only have this function and application? The answer is no, let's take a look. 1. PHP is mainly used in database applications, so there will be a large number of database operations in an application, in the use of object-oriented development (nonsense), if you use the singleton mode, you can avoid a large number of new operations to consume resources. 2. If a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode. This can be see the Frontcontroller section of the Zend Framework. 3. In a page request, it is easy to debug, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo.

http://www.bkjia.com/PHPjc/867244.html www.bkjia.com true http://www.bkjia.com/PHPjc/867244.html techarticle PHP Database Singleton pattern Implementation code sharing, PHP database First we need to know the concept of a single case pattern, then what is a singleton mode? The singleton pattern, as the 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.