簡單實現thinkphp5 instance!

下面由thinkphp教程欄目給大家介紹thinkphp5 instance 的簡單實現方法,希望對需要的朋友有所幫助!

簡單實現thinkphp5 instance!

最近學習 ThinkPHP5,第一次看到 TestClass::instance() 就能創建 TestClass 實例的方法。感到很好奇,翻閱 ThinkPHP 的源代碼,大體理解了 它的 設計思想,非常的先進。

再次從零造車一次(昨天的造車:angularjs的數組傳參方式的簡單實現http://www.miaoqiyuan.cn/p/an…),來講講 他的 具體實現。本文(thinkphp5 instance 的簡單實現)為原創文章,原文地址:http://www.miaoqiyuan.cn/p/ph…,轉載請注明出處。

老規矩,直接上代碼:

立即學習PHP免費學習筆記(深入)”;

<?php class 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 />'; ?>

為了方便調用,也模仿 ThinkPHP 寫了一個助手函數

<?php function app() {     return TestClass::instance(); }   $app2 = app(); $app2->key = 'Application 2'; echo $app2->key . '<br />'; ?>

這樣就簡單的實現了 instance。

不過這種方法還有一個小問題,試想以下,調用100次,就需要創建100個實例,想想都覺得可怕。

給 Test 類 增加一個 靜態屬性,將創建的實例保存到這里。下次如果需要調用,則直接調用這個實例。

<?php class TestClass {       public static $instance; //用于緩存實例       public $data = [];       public static function 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::instance($option); }   header('content-type:text/plain');   $result = []; $app1 = app(); $app1->key = "Application 1"; //修改 key 為 Application 1 $result['app1'] = [     'app1' => $app1->key, //實例中 key 為 Application 1 ];   // 創建 app2,因為 instance 已經存在實例,直接返回 緩存的實例 $app2 = app(); $result['app2'] = [     'setp1' => [         'app1' => $app1->key, // Application 1         'app2' => $app2->key, //因為直接調用的實例的緩存,所以 key 也是 Application 1     ], ];   // 無論 app1,app2 都對在內存中 對應的同一個實例,無論通過誰修改,都能改變值 $app1->key = "Application 2"; $result['app2']['setp2'] = [     'app1' => $app1->key, // Application 2     'app2' => $app2->key, // Application 2 ]; print_r($result); ?>

通過上邊的實驗,可以看到 無論調用多少次,都會使用同一個實例。這樣就解決了效率低的問題。

到現在基本就滿足大多數情況了,唯一的小缺陷,就是 可能 實例的 初始參數不同,這樣沒法靈活調用(常見的比如同一個程序調用兩個數據庫)。在 上邊的 例子中稍作改造,以傳入的參數為key,將不通的 實例緩存到數組中 就可以解決。

<?php class TestClass {       public static $instance = [];   //用于緩存實例數組     public $data = [];       public function __construct($opt = []) {         $this->data = $opt;     }       public static function instance($option = []) {         // 根據傳入的參數 通過 serialize 轉換為字符串,md5 后 作為數組的 key         $instance_id = md5(serialize($option));         //如果 不存在實例,則創建         if (empty(self::$instance[$instance_id])) {             self::$instance[$instance_id] = new self($option);         }         return self::$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 = []; //傳入 初始數據 $app1 = app(['key' => '123']); $result['init'] = $app1->key;    // 使用 傳入的數據,即:123 $app1->key = "app1"; $result['app'] = $app1->key; // 現在值改為了 自定義的 app1了 print_r($result);   $result = []; // 創建 app2,注意 初始參數不一樣 $app2 = app(); // 因為初始參數不一樣,所以還是創建新的實例 $app2->key = "app2"; $result['app1'] = $app1->key;    // app1 $result['app2'] = $app2->key;    // app2 print_r($result);   $result = []; // 創建 app3,傳入的參數 和 app1 一樣,所以會直接返回 和app1相同 的 實例 $app3 = app(['key' => '123']); $result['log'] = [     'app1' => $app1->key, // app1     'app2' => $app2->key, // app2     'app3' => $app3->key, // app1 ];   // 設置 app3 的key,會自動修改 app1 的值,因為他們兩個是同一個實例 $app3->key = 'app3'; $result['app3_set'] = [     'app1' => $app1->key, // app3     'app2' => $app2->key, // app2     'app3' => $app3->key, // app3 ];   // 同理,設置 app1 的key,app3 的 key 也會修改 $app1->key = 'app1'; $result['app1_set'] = [     'app1' => $app1->key, // app1     'app2' => $app2->key, // app2     'app3' => $app3->key, // app1 ]; print_r($result); ?>

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享