高效的PHP自动加载和命名策略 [英] Efficient PHP auto-loading and naming strategies

查看:76
本文介绍了高效的PHP自动加载和命名策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像这些天的大多数Web开发人员一样,我完全享受用于Web应用程序和网站的可靠MVC架构的好处.当使用PHP进行MVC时,自动加载显然非常方便.

Like most web developers these days, I'm thoroughly enjoying the benefits of solid MVC architecture for web apps and sites. When doing MVC with PHP, autoloading obviously comes in extremely handy.

通过简单地定义单个__autoload()函数,我已经成为 spl_autoload_register 的粉丝,因为如果您要合并不同的基本模块(每个模块各自使用各自的自动加载功能),这显然会更加灵活.但是,我从来没有对编写的加载函数感到满意.它们涉及大量的字符串检查和目录扫描,以查找可能要加载的类.

I've become a fan of spl_autoload_register over simply defining a single __autoload() function, as this is obviously more flexible if you are incorporating different base modules that each use their own autoloading. However, I've never felt great about the loading functions that I write. They involve a lot of string checking and directory scanning in order to look for possible classes to load.

例如,假设我有一个应用程序,该应用程序的基本路径定义为PATH_APP,并且具有一个简单的结构,其目录名为modelsviewscontrollers.我经常采用一种命名结构,其中文件在相应目录内分别命名为IndexView.phpIndexController.php,并且默认情况下,模型通常没有特定的方案.我可能对此结构有一个加载器功能,该功能已在spl_autoload_register中注册:

For example, let's say I have an app that has a base path defined as PATH_APP, and a simple structure with directories named models, views and controllers. I often employ a naming structure whereby files are named IndexView.php and IndexController.php inside the appropriate directory, and models generally have no particular scheme by default. I might have a loader function for this structure like this that gets registered with spl_autoload_register:

public function MVCLoader($class)
{
    if (file_exists(PATH_APP.'/models/'.$class.'.php')) {
        require_once(PATH_APP.'/models/'.$class.'.php');
        return true;
    }
    else if (strpos($class,'View') !== false) {
        if (file_exists(PATH_APP.'/views/'.$class.'.php')) {
            require_once(PATH_APP.'/views/'.$class.'.php');
            return true;
        }
    }
    else if (strpos($class,'Controller') !== false) {
        if (file_exists(PATH_APP.'/controllers/'.$class.'.php')) {
            require_once(PATH_APP.'/controllers/'.$class.'.php');
            return true;
        }
    }
    return false;
}

如果在此之后没有找到它,我可能还有另一个功能可以扫描models目录中的子目录.但是,所有的if/else-ing,字符串检查和目录扫描对我来说似乎效率都不高,我想对其进行改进.

If it's not found after that, I might have another function to scan sub-directories in the models directory. However, all the if/else-ing, string checking and directory scanning seems inefficient to me, and I'd like to improve it.

我很好奇其他开发人员可能会采用哪种文件命名和自动加载策略.我正在专门寻找可用于有效自动加载的好的技术,而不是自动加载的替代方法.

I'm very curious what file naming and autoloading strategies other developers might employ. I'm looking specifically for good techniques to employ for efficient autoloading, and not alternatives to autoloading.

推荐答案

这是我在所有项目中一直使用的内容(直接从上一个项目的来源中提取):

This is what I have been using in all of my projects (lifted straight from the source of the last one):

public static function loadClass($class)
{
    $files = array(
        $class . '.php',
        str_replace('_', '/', $class) . '.php',
    );
    foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $base_path)
    {
        foreach ($files as $file)
        {
            $path = "$base_path/$file";
            if (file_exists($path) && is_readable($path))
            {
                include_once $path;
                return;
            }
        }
    }
}

如果我寻找SomeClass_SeperatedWith_Underscores,它将寻找SomeClass_SeperatedWith_Underscores.php,然后寻找根于当前包含路径中每个目录的SomeClass/SeperatedWith/Underscores.php.

If I look for SomeClass_SeperatedWith_Underscores it will look for SomeClass_SeperatedWith_Underscores.php followed by SomeClass/SeperatedWith/Underscores.php rooted at each directory in the current include path.

编辑:我只是想在那里提出,我将其用于提高开发效率,而不必处理时间.如果您的路径上有PEAR,那么您可以使用这些类,而不必在需要时将其包括在内.

I just wanted to put out there that I use this for efficiency in development, and not necessarily processing time. If you have PEAR on your path then with this you can just use the classes and don't have to include them when you need them.

我倾向于将我的类放在目录的层次结构中,并用下划线分隔名称空间...如果需要,此代码可以使文件结构保持整洁,如果需要,可以插入没有嵌套目录的快速类文件想要(用于将一个或两个或两个以上的类添加到要维护的库中,但不属于我当前正在研究的项目的一部分.)

I tend to keep my classes in a hierarchy of directories, with underscores breaking up namespaces... This code lets me keep the file structure nice and tidy if I want, or to inject a quick class file without nested directories if I want (for adding a single class or two to a library that it is defendant on, but not part of the project I am currently working on.)

这篇关于高效的PHP自动加载和命名策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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