好的,当我浏览时,网上有很多帖子和答案。据我了解
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/