This paper mainly introduces the simple realization of ThinkPHP5 instance, which has certain reference value, the small partners of interest can refer to it. We hope to help you.
Recently learned ThinkPHP5, the first time you see Testclass::instance () You can create a TestClass instance of the method. Feel very curious, flip through the source code of thinkphp, generally understand its design ideas, very advanced.
The usual, directly on the code:
<?phpclass TestClass {public static function instance () { return to new Self ();} Public $data = []; Public Function __set ($name, $val) { return $this->data[$name] = $val;} Public Function __get ($name) { return $this->data[$name];}} $app 1 = testclass::instance (); $app 1->key = ' Application 1 '; Echo $app 1->key. ' <br/> ';? >
In order to facilitate the invocation, also imitate Thinkphp wrote an assistant function
<?phpfunction app () {return testclass::instance ();} $app 2 = App (); $app 2->key = ' Application 2 '; Echo $app 2->key. ' <br/> ';? >
This makes it easy to implement the instance.
But this method still has a small problem, think of the following, call 100 times, you need to create 100 instances, think all feel terrible.
Add a static property to the Test class and save the created instance here. The next time a call is needed, the instance is called directly.
<?phpclass TestClass {public static $instance;//For cache 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; The 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 = []; $app 1 = App (); $app 1->key = "Application 1"; Modify key to Application 1$result[' app1 '] = [' app1 ' + = $app 1->key,///instance key is application 1]; Create APP2, because instance already exists instance, directly returns the cached instance $APP2 = App (); $result [' app2 '] = [' setp1 ' + = [' app1 ' = ' 1->key ',// Application 1 ' app2 ' + $app 2->key,//Because the cache of the directly invoked instance, so key is also application 1],]; Regardless of the app1,app2 in memory corresponding to the same instance, regardless of who modified, can change the value $app1->key = "Application 2"; $result [' App2 '] [' setp2 '] = [' app1 ' = = $ap P1->key,//Application 2 ' APP2' = = $app 2->key,//Application 2];p rint_r ($result);? >
From the experiment above, you can see that the same instance will be used no matter how many times it is called. This solves the problem of low efficiency.
Most of the time, the only minor flaw is that the initial parameters of the possible instances are different, so that there is no flexible invocation (more common than a program calls two databases). In the above example, a little modification, with the parameters passed in as key, the non-pass instance is cached into the array can be resolved.
<?phpclass TestClass {public static $instance = [];//For cache instance array public $data = []; Public function __construct ($opt = []) {$this->data = $opt;} public static function instance ($option = []) {//serialize is converted to a string according to the parameters passed in, MD5 as the key of the array $instance _id = MD5 (serial Ize ($option)); If no instance exists, create if (Empty (self:: $instance [$instance _id])) {self:: $instance [$instance _id] = new self ($option); } return Self:: $instance [$instance _id]; The 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 = [];//incoming initial data $APP1 = App ([' key ' = ' 123 ']); $result [' init '] = $app 1->key; Use incoming data, i.e.: 123$app1->key = "App1"; $result [' app '] = $app 1->key; Now the value has changed to the custom App1 Print_r ($result); $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 '] = $app 1->key; app1$result[' app2 ' = $app 2->key; App2print_r ($result); $result = [];//creates app3, passed in parameters like App1, so it returns and App1 the same instance $app3 = App ([' key ' = ' 123 ']); $result [' log '] = [' App1 ' =&G T $app 1->key,//App1 ' app2 ' = $app 2->key,//APP2 ' app3 ' + $app 3->key,//APP1]; Setting APP3 key will automatically modify the value of App1 because both of them are the same instance $app3->key = ' app3 '; $result [' app3_set '] = [' app1 ' = + $app 1->key,//AP P3 ' app2 ' = $app 2->key,//APP2 ' app3 ' + $app 3->key,//APP3]; Similarly, setting App1 's KEY,APP3 key will also modify $app1->key = ' App1 '; $result [' app1_set '] = [' App1 ' and ' = ' 1->key,//App1 ' APP2 ' = = $app 2->key,//APP2 ' app3 ' + $app 3->key,//App1];p rint_r ($result);? >
Related recommendations:
THINKPHP5 uploading images and generating thumbnail image methods
Walkthrough THINKPHP5 URL and Routing features
The most detailed ThinkPHP5 custom paging class tutorial