# CodeIgniter 扩展现有类

**URL:** <https://www.isharkfly.com/t/codeigniter/3620>\
**Category:** PHP\
**Created:** [January 5, 2014, 9:32pm UTC](https://www.isharkfly.com/t/codeigniter/3620 "2014-01-05T21:32:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [January 5, 2014, 9:32pm UTC](https://www.isharkfly.com/t/codeigniter/3620/1 "2014-01-05T21:32:16Z")

</div>

如果你需要在现有类库中加入一两个新的功能,那就完全不必要替换整个类库文件。

你只需简单地扩展(继承)现有的类,扩展一个类就像在类中增加一些例外:

扩展的类必须申明由母类扩展而来.

新扩展的类所在的文件必须以 MY\_ 为前缀(这个选项是可配置的,下面有说明).

例如，要扩展原始类 Email 类你要建立文件 application/libraries/MY\_Email.php, 并按如下方式在文件中声明:[code]class MY\_Email extends CI\_Email {

}[/code]注意:如果你需要在类中使用构造函数,你必须在构造函数中显式继承母类构造函数:[code]class MY\_Email extends CI\_Email {

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

```

}[/code]原文地址：[http://cwiki.ossez.com/pages/viewpage.action?pageId=2392245](http://cwiki.ossez.com/pages/viewpage.action?pageId=2392245)
