如何添加自动加载功能到CodeIgniter? [英] How to add autoload-function to CodeIgniter?
问题描述
我想能够在CodeIgniter的控制器中使用OOP和创建新对象。所以我需要使用一个autoload函数:
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
可能会为记录
但检查你的路径是正确的。此方法还可防止对加载的任何干扰 CI _
libs(意外)
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屋!