PHP Singleton mode in detail, PHP mode in detail _ PHP Tutorial

Source: Internet
Author: User
PHP Singleton mode is described in detail, and PHP mode is described in detail. The PHP Singleton mode is described in detail. The concept of Singleton mode is described in detail in the PHP mode. The Singleton mode refers to the design mode in which a class of the entire application has only one object instance. For details, we will introduce the PHP Singleton mode in detail and the PHP mode in detail.

Concept of Singleton mode

The Singleton mode refers to the design mode in which a class of the entire application has only one object instance. Specifically, as an object creation method, the singleton mode ensures that a class has only one instance, and instantiate it on its own and provides this instance globally to the entire system. It does not create instance copies, but returns a reference to the instance stored in the singleton class.

Features of Singleton mode

The Singleton mode has the following features:Three private and one public":
ThePrivate static member variable
Constructor
It must be declared as private to prevent the external program from losing the meaning of a singleton by creating an object.
Clone functionIt must be declared as private to prevent the object from being cloned.
You must providePublic static method(Usually named getInstance) to return a reference to a unique instance.

Causes and scenarios of Singleton mode

In most PHP applications, a large number of database operations exist. if you do not use the Singleton mode, new operations are required each time. However, each new operation consumes a large amount of system resources and memory resources, in addition, each time you open or close the database, it is a great test and waste for the database. Therefore, the singleton mode is often used in database operations.
Similarly, if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode.

PHP Singleton mode implementation

The following is a framework for implementing database operations in the PHP Singleton mode.

<? Php class Db {const DB_HOST = 'localhost'; const DB_NAME = ''; const DB_USER =''; const DB_PWD = ''; private $ _ db; // save the private static variable private static $ _ instance of the instance; // both the constructor and the clone function are declared as private function _ construct () {// $ this-> _ db = mysql_connect ();} private function _ clone () {// implementation} // public static method for accessing the instance public static function 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 access instance reference $ db = Db: getInstance ();?>

The concept of using Singleton mode refers to the design mode in which a class in the application only has one object instance. Specifically, as...

Related Article

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.