高效的自动加载功能

发布于 2024-10-23 20:17:50 字数 785 浏览 3 评论 0原文

我目前正在构建自己的 PHP 框架,并创建了很多目录来存储我的类。

这是我当前的自动加载功能:

function __autoload($className)
{
    $locations = array('', 'classes/', 'classes/calendar/', 'classes/exceptions/', 'classes/forms/', 'classes/table/', 'classes/user', 'pages/', 'templates/');
    $fileName = $className . '.php';

    foreach($locations AS $currentLocation)
    {
        if(file_exists($currentLocation . $fileName))
        {
            include_once ($currentLocation . $fileName);
            return;
        }
    }
}

现在在我的主类文件中,我确实已经包含了所有必需的类,因此它们不会必须寻找。

我的问题是:

  1. 这个功能足够高效吗?是否会需要很长的加载时间,或者有什么办法可以最大限度地减少加载时间?
  2. include_once() 是我应该包含类的方式吗?
  3. 有没有一种方法可以让我编写函数来猜测最流行的文件夹?或者这会占用太多时间和/或不可能吗?
  4. 命名空间对我有帮助吗? (我现在正在阅读和学习它们。)

I currently am building my own PHP framework and am creating a lot of directories to store my classes in.

This is my current autoload function:

function __autoload($className)
{
    $locations = array('', 'classes/', 'classes/calendar/', 'classes/exceptions/', 'classes/forms/', 'classes/table/', 'classes/user', 'pages/', 'templates/');
    $fileName = $className . '.php';

    foreach($locations AS $currentLocation)
    {
        if(file_exists($currentLocation . $fileName))
        {
            include_once ($currentLocation . $fileName);
            return;
        }
    }
}

Now in my main class file I do have all of the necessary classes already included so that they won't have to be searched for.

Here are my questions:

  1. Is this function efficient enough? Will there be a lot of load time or is there a way for me to minimize the load time?
  2. Is include_once() the way that I should go about including the classes?
  3. Is there a way that I could write the function to guess at the most popular folders? Or would that take up too much time and/or not possible?
  4. Would namespaces help me at all? (I am reading and learning about them right now.)

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

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

发布评论

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

评论(1

漆黑的白昼 2024-10-30 20:17:50
  1. 这里回答得很好:自动加载和多个目录
  2. 您可能应该使用require,因为有两个原因:a) 如果文件已经包含,则不需要 PHP 跟踪,因为如果包含,则不需要首先调用 __autoload;b) 如果无法包含文件,无论如何您都无法继续执行
  3. 第 1 点的答案涵盖了这一点
  4. 不一定;您需要一些类似名称空间的机制来实现更快的加载(仅查看必须查看的位置),但如果需要,您可以在不使用真实名称空间的情况下伪造它。

作为参考,__autoload 和名称空间之间的交互已记录在案< a href="http://us.php.net/manual/en/language.namespaces.rules.php" rel="nofollow noreferrer">此处。

  1. This is answered very well here: autoload and multiple directories
  2. You should probably go with require, for two reasons: a) you don't need to have PHP track if the file has been already included, because if it has it won't need to call __autoload in the first place and b) if the file cannot be included you won't be able to continue execution anyway
  3. The answer for point 1 covers this
  4. Not necessarily; you need some namespace-like mechanism to implement faster loading (to only look where you have to) but you can fake it if necessary without using real namespaces

For reference, the interaction between __autoload and namespaces is documented here.

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