Detailed PHP single-case mode

Source: Internet
Author: User
Singleton mode (Singleton pattern single-piece mode or single-element mode)

Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system.

Singleton mode is a common design pattern, in the computer system, the thread pool, cache, log objects, dialog boxes, printers, database operations, graphics card drivers are often designed as a single case.

The singleton pattern is divided into 3 kinds: Lazy Type single case, a hungry man type single case, registration type single case.

So why use PHP singleton mode?

A major application of PHP is the application of the database to deal with the scene, there will be a large number of database operations in an application, the database handle connection to the database behavior, using a singleton mode can avoid a large number of new operations. Because each new operation consumes both system and memory resources.

Characteristics of the Singleton model

The main feature of the singleton mode is " three private One ":
Requires a private static member variable that holds a unique instance of the class
Constructors
must be declared private to prevent external programs from being new to one object thereby losing the meaning of a singleton
The clone function must be declared private to prevent the object from being cloned
You must provide a public static method that accesses this instance (typically named getinstance), which returns a reference to the unique instance.

Reasons and scenarios for using singleton mode

In most of the PHP application there will be a large number of database operations, if not a singleton mode, that every time to new operations, but each time new will consume a lot of system resources and memory resources, and each open and close the database is a great test and waste of the database. Therefore, singleton patterns are often used in database operation classes.
Similarly, if a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode.

PHP Single-instance mode implementation

The following is a PHP singleton schema implementation of database operation class framework

<?php class db{const db_host= ' localhost '; const db_name= '; const db_user= '; const db_pwd= "; private $_db;//Save Instance Private Static variable private static $_instance; Constructors and clone functions are declared private function construct () {  //$this->_db=mysql_connect ();} Private Function Clone () {  //implementation}//access to the public static method of the instance getinstance () {  if (! Self::$_instance instanceof Self) {  self::$_instance=new self ();  }  or  if (self::$_instance===null) {  self::$_instance=new Db ();  }  return self::$_instance; } public Function Fetchall () {  //implementation} Public function Fetchrow () {  //implementation}}//class external Get instance Reference $db =db::getinstance (); >

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.