将PHP spl_autoload_register()与Codeigniter结合使用 [英] Using the PHP spl_autoload_register() with Codeigniter
问题描述
请问如何在Codeigniter中使用spl_autoload_register()?我需要这样做,因为我使用Codeigniter与另一个框架也使用自动加载。
Please how can I use spl_autoload_register() with Codeigniter? I need to do this because Im using Codeigniter with another framework which also uses autoload.
我在这里看到了一些东西
I saw something here
http://stackoverflow.com/questions/3710480/php-spl-autoload-register
但我不知道如何定位CodeIgniter自动加载。新的OOP和Codeigniter。非常感谢!
but I dont know how to target the CodeIgniter autoload. Im new to OOP and Codeigniter. Thanks a lot!
以上链接具有以下链接:
The above link has this:
function autoload_services($class_name){
$file = 'services/' . $class_name. '.php';
if (file_exists($file)){
require_once($file);
}
}
function autoload_vos($class_name){
$file = 'vos/' . $class_name. '.php';
if (file_exists($file)){
require_once($file);
}
}
function autoload_printers($class_name){
$file = 'printers' . $class_name. '.php';
if (file_exists($file)){
require_once($file);
}
}
spl_autoload_register('autoload_services');
spl_autoload_register('autoload_vos');
spl_autoload_register('autoload_printers');
推荐答案
感谢http://codeigniter.com/forums/viewthread/73804/#366081 以及我在Twitter上关注的一些CI伙伴的一些信息(我问了em): Eric Barnes , Dan Horrigan ,菲尔鲟鱼和 Zack Kitzmiller ,我找到了一个解决方案。如果你是一个CodeIgniter n00b像我一样,你可能会喜欢这些人。
Thanks to http://codeigniter.com/forums/viewthread/73804/#366081 and some bits of information from some CI folk that I follow on twitter (I asked em): Eric Barnes, Dan Horrigan, Phil Sturgeon and Zack Kitzmiller, I found a solution. If you are a CodeIgniter n00b like me, you may like to follow these guys.
我删除了init.php和config.php,然后卡住以下到底我的CI的config.php(我也从一个自定义库mylibrary自动加载)。
I deleted init.php and config.php, then jammed the following into the bottom of my CI's config.php (I am also autoloading from a custom library called mylibrary).
function multi_auto_require($class) {
if(stripos($class, 'CI') === FALSE && stripos($class, 'PEAR') === FALSE) {
foreach (array('flourish', 'mylibrary') as $folder){
if (is_file(APPPATH."../auxengines/{$folder}/{$class}.php")){
include_once APPPATH."../auxengines/{$folder}/{$class}.php";
}
}
}
}
spl_autoload_register('multi_auto_require');
spl_autoload_register('multi_auto_require');
效果非常好。谢谢,人!
Works brilliantly. Thanks, people!
这篇关于将PHP spl_autoload_register()与Codeigniter结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!