我有一组我觉得有用的 PHP 函数。我想为他们创建一个 PSR-4 兼容的存储库,但我找到的指南( 1 、 2 、 3 )似乎只谈论自动加载类。
比如我的文件如下,一个文件一个函数:
my_cool_function1.php
my_cool_function2.php
... etc.
我如何从它们创建符合 PSR-4 的库?
最佳答案
您无法找到任何不是类的 PSR-4 自动加载文件的文档的原因是因为 specification states - 它是为自动加载类而设计的。
直接取自官方规范:
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.
更具体;
The term "class" refers to classes, interfaces, traits, and other similar structures.
具有函数的文件实际上并不是一个相似的结构。
要自动加载这些文件,您需要使用 files
自动加载:
"autoload": {
"files": [
"src/my_cool_function1.php",
"src/my_cool_function2.php"
],
"psr-4": {
"SomeNamespace\\": "src/YourNamespace/"
}
}
你会注意到,psr-4 规范(通常)映射到一个命名空间。
关于php - 一组函数的 PSR-4 目录结构和命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32443327/