PHP Object Single-state (singleton, single-piece) design mode static

Source: Internet
Author: User

<?phpheader (' Content-type:text/html;charset=utf-8 ');/* * single-state design mode   Singleton    single-piece  * a class can only create one object   *1. If you want a class to have only one object, first let the class fail to create the object and construct the method private *2. You can use a static method inside the class to create the object  */     class persion{        //using static members for single-state design          static  $obj  = null;         private function __construct () {                     }         static function getobj () {             //If there is no object at the first call, create, and later invoke, directly use the object created for the first time              if (Is_null (self:: $obj))             self::$ obj = new self;//sElf on behalf of the class name itself             return self:: $obj;         }        function  say () {            echo  ' I am Chinese ';         }                 function __destruct () {             echo  ' Destruction method release Resources <br/> ';         }     }     $p  = persion::getobj ();     $p  = persion::getobj ();     $p  ->say ();


This article is from the "Jin Sha Harbor" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1844656

PHP Object Single-state (singleton, single-piece) design mode static

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.