PHP Single-instance mode

Source: Internet
Author: User

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. <?    classMySQL{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         PublicStaticfunctiongetinstance (){            if(!isset( Self::$instance)){ Self::$instance=New Self();            }            return Self::$instance;        }        //Prevent cloning objects        Privatefunction__clone(){            Trigger_error("Not allow to clone.");         }        functionTest(){            Echo"Test" ;        }    }    $conn= MySQL::getinstance();    $conn -Test();?>

PHP Single-instance mode

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.