首頁 > php框架 > ThinkPHP > 主體

簡單實作thinkphp5 instance!

藏色散人
發布: 2021-03-16 09:01:17
轉載
2317 人瀏覽過

以下由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
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);
?>
登入後複製

以上是簡單實作thinkphp5 instance!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:segmentfault.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!