CodeIgniter 使用手冊版本 2.1.0


建立自己的程式庫

當我們使用"程式庫(Libraries)"這個名稱的時候, 我們通常指的是位在 libraries 目錄底下的類別, 詳細可參考使用手冊中的類別參考的說明。 In this case, however, we will instead describe how you can create your own libraries within your application/libraries directory in order to maintain separation between your local resources and the global framework resources.

As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder.

總結歸納:

底下詳細解釋這三個概念。

Note: The Database classes can not be extended or replaced with your own classes. All other classes are able to be replaced/extended.

存檔位置

自己建立的類別函數可以存放在 application/libraries 資料夾, 當 CodeIgniter 必須初始化建立類別函數, 系統就會來此資料夾查詢並且載入.

命名規則

類別檔

類別檔應該有底下的基本型態。(注意: 底下取名 Someclass 單純只是舉例而已:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Someclass {

    public function some_function()
    {
    }
}

/* End of file Someclass.php */

使用自建類別(Using Your Class)

控制器(Controller) 內的函數載入任何類別, 可使用下列的標準語法:

$this->load->library('someclass');

someclass 的部份, 只是該類別檔案名稱, 不需要包含 ".php" 副檔名。你可以使用大寫或是小寫的檔案名稱, CodeIgniter 都可以接受。

一但載入之後, 你可以取用你的類別採用 小寫 的方式:

$this->someclass->some_function();  // 物件方法名稱必須是小寫

於初使化自定類別時傳遞參數

在初始化類別時, 您可以透過傳遞第二個陣列參數來設定初始建構子:/p> $params = array('type' => 'large', 'color' => 'red');

$this->load->library('Someclass', $params);

當你使用這個特性時, 你必須為類別的建構子加上底下程式碼

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Someclass {

    public function __construct($params)
    {
        // Do something with $params
    }
}

?>

You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder. Note that if you dynamically pass parameters as described above, the config file option will not be available.

在您的程式裡面使用 CodeIgniter 資源

To access CodeIgniter's native resources within your library use the get_instance() function. This function returns the CodeIgniter super object.

Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct:

$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
etc.

$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:

First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

Once you've assigned the object to a variable, you'll use that variable instead of $this:

$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.

注意: You'll notice that the above get_instance() function is being passed by reference:

$CI =& get_instance();

This is very important. Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.

原生程式庫替換至個人版本(Replacing Native Libraries with Your Versions)

Simply by naming your class files identically to a native library will cause CodeIgniter to use it instead of the native one. To use this feature you must name the file and the class declaration exactly the same as the native library. For example, to replace the native Email library you'll create a file named application/libraries/Email.php, and declare your class with:

class CI_Email {

}

Note that most native classes are prefixed with CI_.

To load your library you'll see the standard loading function:

$this->load->library('email');

注意: At this time the Database 類別es can not be replaced with your own versions.

擴充原生程式庫(Extending Native Libraries)

If all you need to do is add some functionality to an existing library - perhaps add a function or two - then it's overkill to replace the entire library with your version. In this case it's better to simply extend the class. Extending a class is nearly identical to replacing a class with a couple exceptions:

For example, to extend the native Email class you'll create a file named application/libraries/MY_Email.php, and declare your class with:

class MY_Email extends CI_Email {

}

注意: If you need to use a constructor in your class make sure you extend the parent constructor:

class MY_Email extends CI_Email {

    public function __construct()
    {
        parent::__construct();
    }
}

載入子類別

To load your sub-class you'll use the standard syntax normally used. DO NOT include your prefix. For example, to load the example above, which extends the Email 類別, you will use:

$this->load->library('email');

Once loaded you will use the class variable as you normally would for the class you are extending. In the case of the email class all calls will use:

$this->email->some_function();

自訂子類別的前置字串

要設定自己的子類別前置字串(sub-class prefix), 請開啟 application/config/config.php 然後找到底下的部份:

$config['subclass_prefix'] = 'MY_';

所有原生的 CodeIgniter 程式庫, 都是使用 CI_ 的前置字串, 所以別把它拿來用了。