如何向 CodeIgniter 添加自动加载功能? [英] How to add autoload-function to CodeIgniter?
问题描述
我希望能够使用 OOP 并在我的 CodeIgniter 控制器中创建新对象.所以我需要使用自动加载功能:
I would like to be able to use OOP and create new objects in my controllers in CodeIgniter. So I need to use an autoload-function:
function __autoload( $classname )
{
require_once("../records/$classname.php");
}
但是如何将其添加到 CodeIgniter 中?
But how can I add that to CodeIgniter?
推荐答案
您可以将上面的自动加载器添加到 app/config/config.php
.我之前在这个位置使用过一个类似的 autoload
功能,它工作得非常好.
You can add your auto loader above to app/config/config.php
. I've used a similar autoload
function before in this location and it's worked quite neatly.
function __autoload($class)
{
if (strpos($class, 'CI_') !== 0)
{
@include_once(APPPATH . 'core/' . $class . EXT);
}
}
由 Phil Sturgeon 提供.这种方式可能更便携.core
对你来说可能是 records
;但无论如何都要检查你的路径是否正确.此方法还可以防止对加载 CI_
库(意外)
Courtesy of Phil Sturgeon. This way may be more portable. core
would probably be records
for you; but check your paths are correct regardless. This method also prevents any interference with loading CI_
libs (accidentally)
这篇关于如何向 CodeIgniter 添加自动加载功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!