PHP spl_autoload_register [英] PHP spl_autoload_register

查看:98
本文介绍了PHP spl_autoload_register的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试利用PHP中的自动加载功能.我在不同目录中有各种类,因此我按如下方式引导自动加载:

I am trying to take advantage of autoloading in PHP. I have various classes in different directories, and so I have bootstrapped the autoloading as follows:

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

一切似乎都很好,但是我只是想仔细检查一下这是否确实可以接受.

It all seems to work fine, but I just wanted to double check that this is indeed considered acceptable practise.

推荐答案

好的,看起来不错.您唯一可以做的就是按照它们最有可能击中的顺序对其进行注册.例如,如果您最常用的类在服务中,然后是vos,然后是打印机,那么您拥有的订单是完美的.这是因为它们已按顺序排队并被调用,因此您可以通过这样做来获得更好的性能.

Sure, looks good. The only thing you might do is register them in the order they're most likely to hit. For example, if your most commonly used classes are in services, then vos, then printers, the order you have is perfect. This is because they're queued and called in-order, so you'll achieve slightly better performance by doing this.

这篇关于PHP spl_autoload_register的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆