欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談thinkphp5 instance 的簡(jiǎn)單實(shí)現(xiàn)

 更新時(shí)間:2017年07月30日 11:05:13   作者:苗啟源  
本篇文章主要介紹了淺談thinkphp5 instance 的簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

老規(guī)矩,直接上代碼:

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

為了方便調(diào)用,也模仿 ThinkPHP 寫了一個(gè)助手函數(shù)

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

這樣就簡(jiǎn)單的實(shí)現(xiàn)了 instance。

不過這種方法還有一個(gè)小問題,試想以下,調(diào)用100次,就需要?jiǎng)?chuàng)建100個(gè)實(shí)例,想想都覺得可怕。

給 Test 類 增加一個(gè) 靜態(tài)屬性,將創(chuàng)建的實(shí)例保存到這里。下次如果需要調(diào)用,則直接調(diào)用這個(gè)實(shí)例。

<?php
class TestClass {
 
 public static $instance; //用于緩存實(shí)例
 
 public $data = [];
 
 public static function instance() {
  //如果不存在實(shí)例,則返回實(shí)例
  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, //實(shí)例中 key 為 Application 1
];
 
// 創(chuàng)建 app2,因?yàn)?instance 已經(jīng)存在實(shí)例,直接返回 緩存的實(shí)例
$app2 = app();
$result['app2'] = [
 'setp1' => [
  'app1' => $app1->key, // Application 1
  'app2' => $app2->key, //因?yàn)橹苯诱{(diào)用的實(shí)例的緩存,所以 key 也是 Application 1
 ],
];
 
// 無論 app1,app2 都對(duì)在內(nèi)存中 對(duì)應(yīng)的同一個(gè)實(shí)例,無論通過誰修改,都能改變值
$app1->key = "Application 2";
$result['app2']['setp2'] = [
 'app1' => $app1->key, // Application 2
 'app2' => $app2->key, // Application 2
];
print_r($result);
?>

通過上邊的實(shí)驗(yàn),可以看到 無論調(diào)用多少次,都會(huì)使用同一個(gè)實(shí)例。這樣就解決了效率低的問題。

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

<?php
class TestClass {
 
 public static $instance = []; //用于緩存實(shí)例數(shù)組
 public $data = [];
 
 public function __construct($opt = []) {
  $this->data = $opt;
 }
 
 public static function instance($option = []) {
  // 根據(jù)傳入的參數(shù) 通過 serialize 轉(zhuǎn)換為字符串,md5 后 作為數(shù)組的 key
  $instance_id = md5(serialize($option));
  //如果 不存在實(shí)例,則創(chuàng)建
  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 = [];
//傳入 初始數(shù)據(jù)
$app1 = app(['key' => '123']);
$result['init'] = $app1->key; // 使用 傳入的數(shù)據(jù),即:123
$app1->key = "app1";
$result['app'] = $app1->key; // 現(xiàn)在值改為了 自定義的 app1了
print_r($result);
 
$result = [];
// 創(chuàng)建 app2,注意 初始參數(shù)不一樣
$app2 = app();
// 因?yàn)槌跏紖?shù)不一樣,所以還是創(chuàng)建新的實(shí)例
$app2->key = "app2";
$result['app1'] = $app1->key; // app1
$result['app2'] = $app2->key; // app2
print_r($result);
 
$result = [];
// 創(chuàng)建 app3,傳入的參數(shù) 和 app1 一樣,所以會(huì)直接返回 和app1相同 的 實(shí)例
$app3 = app(['key' => '123']);
$result['log'] = [
 'app1' => $app1->key, // app1
 'app2' => $app2->key, // app2
 'app3' => $app3->key, // app1
];
 
// 設(shè)置 app3 的key,會(huì)自動(dòng)修改 app1 的值,因?yàn)樗麄儍蓚€(gè)是同一個(gè)實(shí)例
$app3->key = 'app3';
$result['app3_set'] = [
 'app1' => $app1->key, // app3
 'app2' => $app2->key, // app2
 'app3' => $app3->key, // app3
];
 
// 同理,設(shè)置 app1 的key,app3 的 key 也會(huì)修改
$app1->key = 'app1';
$result['app1_set'] = [
 'app1' => $app1->key, // app1
 'app2' => $app2->key, // app2
 'app3' => $app3->key, // app1
];
print_r($result);
?>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論