PHP's SingleInstance

Source: Internet
Author: User

1<?PHP2     //Singleton: A class has only one object. 3 //Restrict the class from getting more objects, allowing only one object to be generated. , an object can accomplish all the functions of the object in order to save costs. This is the singleton pattern .4     5     /*6 function func () {7 static $name;//local static variable, calculated according to the function life cycle, the next call will continue to exist,8         }9         Ten class test{ One static $name;//belongs to the Class property.  A         } -  -     */ the  -     classmysqldb{ -         //declaring a static variable storage object -         Private Static $_instance; +  -         //the declaration is constructed as private and does not allow the outside new object +         Private function__construct () { A  at         } -  -         //given a class method that does not allow the class to be accessed directly from the class name -          Public Static functiongetinstance () { -  -             //To determine whether an object is instantiated in             if(!isset(Static::$_instance)) { -                 //if (! (static::$_instance instanceof Static)) Using the object transport symbol instanceof to make judgments to //Does not exist object, new object and returns to private static variable +                 Static::$_instance=New Static; -             } the  *             //return to static variable storage $             return Static::$_instance;Panax Notoginseng         } -  the         //in order to prevent the external cloning of the private magic method +         Private function__clone () { A  the         } +     } -  $  $     $dao= MySQLdb::getinstance (); -  -     Var_dump($dao); the  -     $dao 1= MySQLdb::getinstance ();Wuyi  the     Var_dump($dao 1); -  Wu     //$dao 2 = Clone $dao; Run Save/**/

PHP's SingleInstance

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.