CI3驅動器(drivers)建立與使用

哆啦啊梦發表於2024-07-05

一、建立檔案和目錄結構

/application/libraries/Driver_name            
Driver_name.php  //為了在大小寫敏感的檔案系統下保證相容性,Driver_name 目錄必須以 ucfirst() 函式返回的結果格式進行命名。
drivers
Driver_name_subclass_1.php
Driver_name_subclass_2.php
Driver_name_subclass_3.php

二、使用驅動器

$this->load->driver('class_name');      //載入驅動器



$this->some_parent->some_method();      //呼叫父類方法



$this->some_parent->child_one->some_method();      //呼叫子類方法
$this->some_parent->child_two->another_method();

三、例項

1、建立mine驅動器類和子類Mine_abc.php

Mine.php

<?php

class Mine extends CI_Driver_Library
{

    protected $valid_drivers = array(
        'abc'              //註冊驅動器子類(必須)
    );


    function kk()
    {
        echo 222;
    }
}

Mine_abc.php

<?php

class CI_Mine_abc extends CI_Driver
{


    function dd()
    {
        echo 333;
    }
}

呼叫

public function index()
    {
        $this->load->driver('mine');
        $this->mine->kk();
        $this->mine->abc->dd();        
    }

相關文章