将 PHP spl_autoload_register() 与 Codeigniter 结合使用

发布于 2024-09-25 22:16:54 字数 942 浏览 0 评论 0原文

请问如何将 spl_autoload_register() 与 Codeigniter 一起使用?我需要这样做,因为我将 Codeigniter 与另一个也使用自动加载的框架一起使用。

我在这里看到了一些东西

PHP spl_autoload_register

我不知道如何定位 CodeIgniter 自动加载< /强>。我是 OOP 和 Codeigniter 的新手。多谢!

上面的链接有这个:

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');

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

PHP spl_autoload_register

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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-10-02 22:16:54

感谢 http://codeigniter.com/forums/viewthread/73804/#366081以及我在 Twitter 上关注的一些 CI 人员提供的一些信息(我问他们):Eric Barnes丹·霍里根菲尔·斯特金< /a> 和 Zack Kitzmiller,我找到了解决方案。如果您是像我一样的 CodeIgniter n00b,您可能会喜欢关注这些人。

我删除了 init.php 和 config.php,然后将以下内容塞入 CI 的 config.php 底部(我还从名为 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');

工作出色。谢谢大家!

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.

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');

Works brilliantly. Thanks, people!

山有枢 2024-10-02 22:16:54

啊啊,我现在明白了(在查看之前的问题之后你问过)...你遇到了问题,因为有 2 个定义的 __autoload 函数(因此导致解析错误)...

要修复它,只需将其中一个重命名为其他名称,然后在定义之后调用 spl_autoload_register('yournewfunctionname');...

只要我理解你的问题,这应该就是全部内容了...

Ahhh, I see now (after looking at a prior question you asked)... You're having a problem because there are 2 defined __autoload functions (and hence result in a parse error)...

To fix it, simply rename one of them to something else, and then right after definition call spl_autoload_register('yournewfunctionname');...

That should be all there is to it, so long as I understand your problem...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文