php - Psr-4 : namespace and spl autoload

标签 php namespaces spl-autoload-register

好的,当我浏览时,网上有很多帖子和答案。据我了解

psr-0:仅 SPL 自动加载

psr-4:SPL 自动加载 + 命名空间

大多数答案都包含 LONG 方法。 psr-4 应该不难实现,因为它的目的是简化文件结构,同时保留其自身的优势。

我有这样的结构 project\view\main.php
main.php

namespace project\view;
class main {
    .......
}

在作为我的根目录的项目文件夹之外,我有一个 index.php
spl_autoload_register( function ($ClassName) {
    require $ClassName . '.php';
});
$main = new project\view\main();

问题:我用 psr-4 做对了吗,还是我仍然缺少文档中的某些内容?

最佳答案

来自规范PSR-0 [1]已弃用(自 2014-10-21 起)并由 PSR-4 取代

来自 PSR-4文档:

This PSR describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be autoloaded according to the specification.



http://www.php-fig.org/psr/psr-4/

如果您想了解规范更改的完整理由,您可以查阅 PSR-4 meta document .

如果您想查看工作示例,可以在 same location 中搜索

我的最终建议是查看 composer并让它处理它。您只需要包含自动生成的 autoload.php文件具有功能齐全的自动加载。

关于php - Psr-4 : namespace and spl autoload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714962/

相关文章:

php - 如何确定 memcached 中的数据何时到期?

php - 无法将 $_POST 变量复制到常规变量中

php - class_exists 正在调用 spl_autoload_register

PHP 最终阻止自动加载中止

php - 用户可以在浏览器开发工具中操作选择框值吗

php - 无法使用提供的设置连接到数据库服务器。在代码点火器上

c++ - 写 "::namespace::identifier"和 "namespace::identifier"有什么区别?

c# - 命名空间中不存在 Blazor 命名空间互操作

php - 未找到 Drupal 类