PHP design mode single-instance mode

Source: Internet
Author: User

The singleton pattern (Singleton pattern) is a creation pattern that restricts the application to create only a single instance of a class type. For example, a Web site will need a database connection object, but there should be only one, so we need to use Singleton mode to implement.

eg

<?PHPclassconfig{Static Private $_instance=NULL;//ensure that there is only one single instance for a particular class    Private $_settings=Array(); Private function__construct () {}//prevent new instances from coming out    Private function__clone () {}//prevent clone from creating a new instance    Staticfuncitn getinstance () {if(Self::$_instance==NULL) { self::$_instance=NewConfig (); }          returnSelf::$_instance; }    functionSet$index,$value){         $this->_settings[$index] =$value; }    functionGet$index){          return $this->_settings[$index]; }}?>

Using the Config class

Creating the Config Class

$CONFIG = Config::getinstance ();

If this object is the first object of the class, a new instance of the class is created, assigned to the internal private property $_instance, and the result is returned. Otherwise, use the same instance to return

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.