久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

thinkphp5中如何使用instance-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)thinkphp5中如何使用instance,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計、網(wǎng)站維護、公眾號搭建、小程序設(shè)計、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動行銷領(lǐng)域創(chuàng)造價值而不懈努力!

代碼:


 <?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 寫了一個助手函數(shù)

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

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

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

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

<?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
];
 
// 創(chuàng)建 app2,因為 instance 已經(jīng)存在實例,直接返回 緩存的實例
$app2 = app();
$result['app2'] = [
 'setp1' => [
  'app1' => $app1->key, // Application 1
  'app2' => $app2->key, //因為直接調(diào)用的實例的緩存,所以 key 也是 Application 1
 ],
];
 
// 無論 app1,app2 都對在內(nèi)存中 對應(yīng)的同一個實例,無論通過誰修改,都能改變值
$app1->key = "Application 2";
$result['app2']['setp2'] = [
 'app1' => $app1->key, // Application 2
 'app2' => $app2->key, // Application 2
];
print_r($result);
?>

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

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

<?php
class TestClass {
 
 public static $instance = []; //用于緩存實例數(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));
  //如果 不存在實例,則創(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();
// 因為初始參數(shù)不一樣,所以還是創(chuàng)建新的實例
$app2->key = "app2";
$result['app1'] = $app1->key; // app1
$result['app2'] = $app2->key; // app2
print_r($result);
 
$result = [];
// 創(chuàng)建 app3,傳入的參數(shù) 和 app1 一樣,所以會直接返回 和app1相同 的 實例
$app3 = app(['key' => '123']);
$result['log'] = [
 'app1' => $app1->key, // app1
 'app2' => $app2->key, // app2
 'app3' => $app3->key, // app1
];
 
// 設(shè)置 app3 的key,會自動修改 app1 的值,因為他們兩個是同一個實例
$app3->key = 'app3';
$result['app3_set'] = [
 'app1' => $app1->key, // app3
 'app2' => $app2->key, // app2
 'app3' => $app3->key, // app3
];
 
// 同理,設(shè)置 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);
?>

關(guān)于“thinkphp5中如何使用instance”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

網(wǎng)頁名稱:thinkphp5中如何使用instance-創(chuàng)新互聯(lián)
分享路徑:http://sd-ha.com/article22/djpejc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航企業(yè)建站、品牌網(wǎng)站設(shè)計網(wǎng)站排名品牌網(wǎng)站建設(shè)、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計