Psr-4:命名空间和 spl 自动加载
Psr-4 : namespace and spl autoload
好吧,当我四处浏览时,网上有很多帖子和答案。据我了解
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
文件即可实现功能齐全的自动加载。
好吧,当我四处浏览时,网上有很多帖子和答案。据我了解
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
文件即可实现功能齐全的自动加载。