Simple implementation of thinkphp5 instance, thinkphp5instance
When I recently learned ThinkPHP5, I first saw the TestClass: instance () method to create a TestClass instance. Curious, I read the source code of ThinkPHP and understood its design idea in general. It is very advanced.
Old Rules: go directly to the Code:
<?phpclass TestClass { public static function instance() { return new self(); } public $data = []; public function __set($name, $val) { return $this->data[$name] = $val; } public function __get($name) { return $this->data[$name]; }} $app1 = TestClass::instance();$app1->key = 'Application 1';echo $app1->key . '<br />';?>
In order to facilitate the call, I wrote an assistant function in imitation of ThinkPHP.
<?phpfunction app() { return TestClass::instance();} $app2 = app();$app2->key = 'Application 2';echo $app2->key . '<br />';?>
In this way, the instance is simply implemented.
However, there is still a small problem with this method. Imagine that if you call 100 times, you need to create 100 instances. It is terrible to think about it.
Add a static attribute to the Test class and save the created instance here. If you need to call the instance next time, you can call the instance directly.
<? Phpclass TestClass {public static $ instance; // used to cache the instance public $ data = []; public static function instance () {// if no instance exists, returns the instance if (empty (self: $ instance) {self: $ instance = new self ();} return self: $ instance ;} public function _ set ($ name, $ val) {return $ this-> data [$ name] = $ val;} public function _ get ($ name) {return $ this-> data [$ name] ;}} function app ($ option = []) {return TestClass: insta Nce ($ option);} header ('content-type: text/plain '); $ result = []; $ app1 = app (); $ app1-> key = "Application 1"; // modify key to Application 1 $ result ['app1'] = ['app1' => $ app1-> key, // The key in the instance is Application 1]; // create app2. Because the instance already exists, the cached instance $ app2 = app () is directly returned (); $ result ['app2'] = ['setp1' => ['app1' => $ app1-> key, // Application 1 'app2' => $ app2-> key, // because the cache of the Instance called directly, the key is also Application 1],]; // regardless of Pp1 and app2 change the value of $ app1-> key = "Application 2" for the same instance in the memory "; $ result ['app2'] ['setp2'] = ['app1' => $ app1-> key, // Application 2 'app2' => $ app2-> key, // Application 2]; print_r ($ result);?>
The experiment above shows that the same instance is used no matter how many calls are made. This solves the problem of low efficiency.
By now, it has basically met most situations. The only small defect is that the initial parameters of the instance may be different, which cannot be called flexibly (for example, the same program calls two databases ). In the above example, we will make a slight transformation and use the input parameter as the key to cache inaccessible instances into the array.
<? Phpclass TestClass {public static $ instance = []; // used to cache the instance array public $ data = []; public function _ construct ($ opt = []) {$ this-> data = $ opt;} public static function instance ($ option = []) {// converts a string using serialize based on input parameters, after md5 is used as the key $ instance_id = md5 (serialize ($ option) of the array; // if no instance exists, create if (empty (self :: $ instance [$ instance_id]) {self: $ instance [$ instance_id] = new self ($ option);} return sel F: $ instance [$ instance_id];} public function _ set ($ name, $ val) {return $ this-> data [$ name] = $ val ;} public function _ get ($ name) {return $ this-> data [$ name] ;}} function app ($ option = []) {return TestClass :: instance ($ option);} header ('content-type: text/plain '); $ result = []; // input initial data $ app1 = app (['key' => '000000']); $ result ['init '] = $ app1-> key; // use the input data, that is, 123 $ app1-> key = "app1"; $ result ['app'] = $ App1-> key; // The current value is changed to print_r ($ result) for the custom app1; $ result = []; // create app2, note that the initial parameters are different $ app2 = app (); // because the initial parameters are different, create a new instance $ app2-> key = "app2 "; $ result ['app1'] = $ app1-> key; // app1 $ result ['app2'] = $ app2-> key; // app2print_r ($ result ); $ result = []; // create app3. The input parameter is the same as app1, therefore, the instance $ app3 = app (['key' => '20140901']) that is the same as app1 is directly returned. $ result ['log'] = ['app1' => $ app1-> key, // app1 'app2' => $ app2-> key ,// App2 'app3' => $ app3-> key, // app1]; // set the key of app3, and the value of app1 is automatically modified, because the two of them are the same instance $ app3-> key = 'app3'; $ result ['app3 _ set'] = ['app1' => $ app1-> key, // app3 'app2' => $ app2-> key, // app2 'app3' => $ app3-> key, // app3]; // likewise, set the app1 key. The app3 key also changes $ app1-> key = 'app1 '; $ result ['app1 _ set'] = ['app1' => $ app1-> key, // app1 'app2' => $ app2-> key, // app2 'app3' => $ app3-> key, // app1]; print_r ($ result) ;?>
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.