Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 24 additions & 45 deletions source/general/core_classes.rst
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
############################
Creating Core System Classes
新增系統核心類別
############################

Every time CodeIgniter runs there are several base classes that are
initialized automatically as part of the core framework. It is possible,
however, to swap any of the core system classes with your own versions
or even extend the core versions.
每次 CodeIgniter 執行時,有幾個基礎類別會自動初始化,成為核心框架的一部分。不過還是有可能用你自己的版本來置換甚至擴充這些系統核心類別。

**Most users will never have any need to do this, but the option to
replace or extend them does exist for those who would like to
significantly alter the CodeIgniter core.**
**大部分的使用者永遠不會需要這樣做,但是這個選項還是保留給那些想要大幅置換或擴充 CodeIgniter 核心的人。**

.. note:: Messing with a core system class has a lot of implications, so
make sure you know what you are doing before attempting it.
.. note:: 介入核心系統可能引發許多隱藏的問題,所以請在這麼做之前確定你知道你自己在做什麼。

System Class List
系統類別列表
=================

The following is a list of the core system files that are invoked every
time CodeIgniter runs:
以下是一個系統核心檔案的清單,每次 CodeIgniter 執行時都會呼叫他們: :

- Benchmark
- Config
Expand All @@ -35,48 +28,40 @@ time CodeIgniter runs:
- URI
- Utf8

Replacing Core Classes
更換核心類別
======================

To use one of your own system classes instead of a default one simply
place your version inside your local *application/core/* directory::
要用你自己的系統核心類別來置換預設的,只要將你自己的版本放在 *application/core* 目錄裡: ::

application/core/some_class.php

If this directory does not exist you can create it.
如果這個目錄不存在,你可以自己新增。

Any file named identically to one from the list above will be used
instead of the one normally used.
任何與在上述清單中檔案名稱相符的檔案都會被載入,取代系統正常使用的。

Please note that your class must use CI as a prefix. For example, if
your file is named Input.php the class will be named::
請注意,你的類別名稱必須使用 CI 作為前置字串。例如,如果你的檔案名稱是 Input.php ,那類別名稱將是: ::

class CI_Input {

}

Extending Core Class
繼承核心類別
====================

If all you need to do is add some functionality to an existing library -
perhaps add a method 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:
如果你所要做的只是在現存程式庫中加入一些功能,例如增加一兩個函數,那置換掉整個程式庫就太過火了。在這個狀況下,擴充類別是比較好的做法。擴充一個類別與用一些例外來取代一個類別幾乎是相同的: :

- The class declaration must extend the parent class.
- Your new class name and filename must be prefixed with MY\_ (this
item is configurable. See below.).
- 類別宣告必須繼承(extend)父類別
- 你的新類別名稱與檔名必須使用 MY\_ 前置字串(這是可設定的,請見下述)。

For example, to extend the native Input class you'll create a file named
application/core/MY_Input.php, and declare your class with::
例如,要擴充一個內建的 Input 類別,你要先新增檔名為 application/core/MY_Input.php 的檔案,然後這樣宣告你的類別: ::

class MY_Input extends CI_Input {

}

.. note:: If you need to use a constructor in your class make sure you
extend the parent constructor::
.. note:: 如果你需要在你的類別中使用建構函數(Constructor),確定有在裡面擴充父類別的建構函數(Constructor):

::

class MY_Input extends CI_Input {

Expand All @@ -86,13 +71,9 @@ application/core/MY_Input.php, and declare your class with::
}
}

**Tip:** Any functions in your class that are named identically to the
methods in the parent class will be used instead of the native ones
(this is known as "method overriding"). This allows you to substantially
alter the CodeIgniter core.
**Tip:** 你類別中任何與父類別中相同名稱的函數,將會取代父類別的(這一般叫做“方法覆蓋 method overriding”)。這個方式允許你實質上改變 CodeIgniter 的核心。

If you are extending the Controller core class, then be sure to extend
your new class in your application controller's constructors.
如果你要擴充控制器(Controller)核心類別,請確定有在你的應用程式控制器(Controller)的建構函數(Constructor)中擴充你的新類別。

::

Expand All @@ -109,13 +90,11 @@ your new class in your application controller's constructors.
}
}

Setting Your Own Prefix
自訂子類別的前綴字串
-----------------------

To set your own sub-class prefix, open your
*application/config/config.php* file and look for this item::
要設定你自己的子類別前置字串,請編輯你的 *application/config/config.php* 檔案並修改下面的項目: ::

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

Please note that all native CodeIgniter libraries are prefixed
with CI\_ so DO NOT use that as your prefix.
請注意,所有的 CodeIgniter 內建程式庫都使用 CI\_ 前綴字串,所以不要用它來當作你自己的前綴字串。