<?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