Php Singleton mode, php mode_php tutorial

Source: Internet
Author: User
Php Singleton mode and php mode. The php Singleton mode and the php Singleton mode are mainly used for database connection. make sure that there is only one instance for a database class and this instance is provided to the entire system. This avoids new php Singleton mode and php mode.
The Singleton mode is mainly used for database connection. it ensures that a database class has only one instance and provides this instance to the entire system. In this way, the new operation can avoid resource consumption and the too transaction connection information in the database. There are three key points: 1. there must be only one instance. 2. the instance must be created automatically. 3. you must provide this instance to the entire system. ClassMysql {privete Static$ Instance; // Save the instance // The constructor declaration as private to prevent direct creation of the object privete Function _ Construct() {// Instantiate} // Singleton method to determine whether the instance has been instantiated, only once Public Static FunctionGetInstance (){ If(! Isset(Self: $ instance) {self: $ instance = NewSelf ();} ReturnSelf: $ instance;} // prevents cloned objects Private Function_ Clone (){ Trigger_error("Not allow to clone .");} FunctionTest (){ Echo"Test" ;}}$ conn = mysql: getInstance (); $ conn-> test ();?>

The Butler Singleton mode is mainly used for database connection. it ensures that a database class has only one instance and provides this instance to the entire system. To avoid new operations...

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.