PHP singleton mode, PHP mode _php tutorial

Source: Internet
Author: User

PHP singleton mode, PHP mode


The singleton mode is primarily used for database connections, ensuring that the database has only one instance of a class and provides this instance to the entire system. This prevents new operations from consuming resources while avoiding too many connection information in the database. There are three points: 1. There must be only one instance. 2. This instance must be created automatically. 3. This instance must be provided to the system as a whole. class MySQL{Privete Static $instance ;//Save Instance The //constructor is declared private to prevent the object from being created directlyPrivete function __construct(){ //Instantiation } //Singleton method to determine if it has been instantiated, and only instantiate once Public Static function getinstance (){ if(! isset( Self::$instance )){ Self ::$instance = New Self(); } return Self:: $instance; } //Prevent cloning objects Private function __clone (){ Trigger_error ("Not allow to clone."); } function Test(){ Echo "Test" ; } } $conn = MySQL::getinstance (); $conn -Test ();?>

http://www.bkjia.com/PHPjc/955696.html www.bkjia.com true http://www.bkjia.com/PHPjc/955696.html techarticle php singleton mode, PHP mode singleton mode is mainly used in database connection, ensure that the database has only one instance of a class, and provide this instance to the whole system. Thus avoiding the new ...

  • 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.