PHP作曲家自动加载classmap和文件之间的差异

发布于 2025-02-07 09:37:32 字数 604 浏览 2 评论 0 原文

属性 classmap vs. 文件的自动加载的区别是什么?

他们都做同样的事情并以同样的方式工作?

{   
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Computer\\Automobile\\": "automobile/"
        },
        "classmap": [
            "classes/automobile.php"
        ],
        "files": [
            "files/automobile.php"
        ]
    }
}

我知道您的PSR-4标准需要您基于名称空间的严格文件系统结构。假设您在 src 中有一个 app ,带有 app app namespace的目录,所有子名称空间都将镜像子目录和类名称为与没有 .php 扩展名的文件名相同。

What is the difference on autoload of property classmap vs. files.

They both makes the same thing and work the same way?

{   
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Computer\\Automobile\\": "automobile/"
        },
        "classmap": [
            "classes/automobile.php"
        ],
        "files": [
            "files/automobile.php"
        ]
    }
}

I know the PSR-4 standard requires of you a strict filesystem structure based on namespaces. Say you have an app directory in the src directory with App namespace, then all sub-namespaces will mirror sub-directories and class names will be the same as file names without the .php extension.

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

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

发布评论

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

评论(1

晚雾 2025-02-14 09:37:32

这些记录在

那么有什么区别。我会说你们彼此之间无法很好地比较。与 PSR-4 相比,认为您可以使用 classmap ,然后指向目录。然后,作曲家将通过 dump-autoload 时间进行工作。在构建项目包进行部署时,这可能很有用。

对于开发, PSR-4 更好,因为您不需要 dump-autoload 当您添加新类或重命名一个等

。案例是要配置 PSR-4 。然后,当您构建用于部署的软件包时,您将使用Build-In自动加载优化。它将自动将 classmap 作为 psr-4 零件如生产中所示,我们不希望代码更改(classmap不得检查文件 - 系统(磁盘i/o!)不再存在,因为它在内存中)。当时使Classmap的权威成为授权,即使是倒塌,也不会再检查文件系统。

使用所有这些选项以使您受益,无论是开发还是在生产中进行开发:

  • 默认情况下使用 psr-4
  • 使用 classmap 如果没有其他好选项(例如,不支持 psr-4 / psr-0 的代码(检查 psr) -0 适合
  • 在您需要的情况下使用。
  • files 自动加载器优化。

Those are documented in autoload reference, in short:

  • classmap: point to one or more files or directories. when you execute composer dump-autoload (implicitly with composer install and composer update as well), Composer will scan these path(s) for PHP files for classes/interfaces/traits/enums and creates a mapping for those fully qualified names to the files pathnames that contain them. When the autoloader kicks in, that classmap is checked and then the file required if in the map. You can make the classmap authorative, it is highly versatile (compare Autoloader optimization).

  • files: point to one or more files. strictly speaking this is not a class autoloader (compare spl_autoload() etc.). All files pointed to will be loaded (required) when the composer autoloader is required (vendor/autoload.php). You can use it to load files that define functions as no autoloading exists for that. It is also often used to create shims (conditionally defined functions or classes if they don't exist, alias classes etc.).

So what is the difference. I'd say those you can't compare very well to each other. Compared with psr-4 thought you could instead use classmap and just point to the directory. Composer would then do the work at dump-autoload time. This can be useful when building the projects package for deployment.

For development, psr-4 is better as you don't need to dump-autoload all the time when you add a new class or rename one etc.

A common use-case is to have psr-4 therefore configured. Then when you build the package for deployment, you make use of the build-in autoload optimization. It will automatically dump a classmap for the psr-4 parts as in production we don't expect the code to change any longer (and the classmap must not check the file-system any longer (disk i/o!) as it is in memory). Making the classmap authorative then even means, the file-system won't be checked any longer even as a fall-back.

Use all of these options to your benefit, be it development or later on in production:

  • Go with psr-4 by default.
  • Use classmap if there is no other good option (e.g. code that does not support psr-4 / psr-0 (check psr-0 if it suits, it is pretty lax in Composer).
  • files is for file includes. You normally know when you need it. Should not be used.
  • Then in your build configuration do the autoloader optimization.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文