最近學習 ThinkPHP5,第一次看到 TestClass::instance() 就能創建 TestClass 實例的方法。感到很好奇,翻閱 ThinkPHP 的源碼,大體理解了它的 設計思想,非常的先進。
代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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 寫了一個助手函數
1 2 3 4 5 6 7 8 9 | <?php function app() { return TestClass::instance(); } $app2 = app(); $app2 ->key = 'Application 2' ; echo $app2 ->key . '<br />' ; ?> |
這樣就簡單的實現了 instance。
不過這種方法還有一個小問題,試想以下,調用100次,就需要創建100個實例,想想都覺得可怕。
給 Test 類 增加一個 靜態屬性,將創建的實例保存到這裏。下次如果需要調用,則直接調用這個實例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <?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,將不通的 實例緩存到數組中 就可以解決。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <?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 ); ?> |