[英]What Are the Differences Between PSR-0 and PSR-4?
Recently I've read about namespaces and how they are beneficial. 最近我读到了名称空间以及它们如何有益。 I'm currently creating a project in Laravel and trying to move from class map autoloading to namespacing.
我目前正在Laravel中创建一个项目,并试图从类地图自动加载转移到命名空间。 However, I can't seem to grasp what the actual difference is between PSR-0 and PSR-4.
但是,我似乎无法掌握PSR-0和PSR-4之间的实际差异。
Some resources that I've read are... 我读过的一些资源是......
What I understand: 我的理解:
Examples explaining the difference would be appreciated. 可以理解解释差异的例子。
They are very similar so it is not surprising that it's a bit confusing. 它们非常相似,所以它有点令人困惑也就不足为奇了。 The summary is that PSR-0 had some backwards compatibility features for PEAR-style classnames that PSR-4 dropped, as such it only supports namespaced code.
总结是PSR-0具有PSR-4丢弃的PEAR样式类名的一些向后兼容性功能,因此它只支持命名空间代码。 On top of that PSR-4 does not force you to have the whole namespace as a directory structure, but only the part following the anchor point.
最重要的是,PSR-4不会强制您将整个命名空间作为目录结构,而只是使用锚点之后的部分。
For example if you define that the Acme\\Foo\\
namespace is anchored in src/
, with PSR-0 it means it will look for Acme\\Foo\\Bar
in src/Acme/Foo/Bar.php
while in PSR-4 it will look for it in src/Bar.php
, allowing for shorter directory structures. 例如,如果您定义
Acme\\Foo\\
名称空间锚定在src/
,使用PSR-0意味着它将在src/Acme/Foo/Bar.php
中查找Acme\\Foo\\Bar
,而在PSR-4中它将在src/Bar.php
查找它,允许更短的目录结构。 On the other hand some prefer to have the full directory structure to clearly see what is in which namespace, so you can also say that Acme\\Foo\\
is in src/Acme/Foo
with PSR-4 which will gives you the equivalent of the PSR-0 behavior described above. 另一方面,有些人更喜欢使用完整的目录结构来清楚地看到哪个命名空间,所以你也可以说
Acme\\Foo\\
在src/Acme/Foo
有PSR-4,这将给你相当于上面描述的PSR-0行为。
Long story short for new projects and for most intents and purposes, you can use PSR-4 and forget all about PSR-0. 对于新项目以及大多数意图和目的而言,您可以使用PSR-4而忘记PSR-0的所有内容。
Here are the major differences, 这是主要的区别,
1. For example if you define that the Acme\\Foo\\
namespace is anchored in src/
, 1.例如,如果您定义
Acme\\Foo\\
命名空间锚定在src/
,
Acme\\Foo\\Bar
in src/Acme/Foo/Bar.php
src/Acme/Foo/Bar.php
查找Acme\\Foo\\Bar
Acme\\Foo\\Bar
in src/Bar.php(where Bar class is)
. src/Bar.php(where Bar class is)
查找Acme\\Foo\\Bar
。 2. PSR-4 does not convert underscores to directory separators 2. PSR-4不会将下划线转换为目录分隔符
3. You would prefer using PSR-4 with namespaces 3.您更希望将PSR-4与命名空间一起使用
4. PSR-0 will not work even if the class name is different from file name, like considering above example: 4.即使类名与文件名不同,PSR-0也不起作用,如上面的例子:
Acme\\Foo\\Bar
---> src/Acme/Foo/Bar.php
(for Bar class) will work Acme\\Foo\\Bar
---> src/Acme/Foo/Bar.php
(适用于Bar类)可以使用 Acme\\Foo\\Bar
---> src/Acme/Foo/Bar2.php
(for Bar class) will not work Acme\\Foo\\Bar
---> src/Acme/Foo/Bar2.php
(适用于Bar类)不起作用 PSR-4 is something like 'relative path', PSR-0, 'absolute path'. PSR-4就像'相对路径',PSR-0,'绝对路径'。
eg 例如
config: 配置:
'App\Controller' => 'dir/'
PSR-0 autoload: PSR-0自动加载:
App\Controller\IndexController --> dir/App/Controller/IndexController.php
PSR-4 autoload: PSR-4自动加载:
App\Controller\IndexController --> dir/IndexController.php
And there are some more difference in details between PSR-0 and PSR-4, see here: http://www.php-fig.org/psr/psr-4/ PSR-0和PSR-4之间的细节有所不同,请参见此处: http : //www.php-fig.org/psr/psr-4/
Namespace/ folder convention. 命名空间/文件夹约定。
Classes should be stored in folders according to their namespaces. 应根据命名空间将类存储在文件夹中。
In general, you will create an src/ directory in your root folder, sitting at the same level as vendor/, and add your projects there. 通常,您将在根文件夹中创建一个src /目录,与vendor /位于同一级别,并在那里添加项目。 Below is an example of the folder structure:
下面是文件夹结构的示例:
.
+-- src
|
+-- Book
| +-- History
| | +-- UnitedStates.php - namespace Book\History;
+-- Vehicle
| +-- Air
| | +-- Wings
| | | +-- Airplane.php - namespace Vehicle\Air\Wings;
| +-- Road
| | +-- Car.php - namespace Vehicle\Road;
+-- tests
+-- test.php
+-- vendor
Difference between psr-0 and psr-4 psr-0和psr-4之间的区别
psr-0 PSR-0
It is deprecated. 它已被弃用。 Looking at
vendor/composer/autoload_namespaces.php
file you can see the namespaces and the directories that they are mapped to. 查看
vendor/composer/autoload_namespaces.php
文件,您可以看到名称空间及其映射到的目录。
composer.json composer.json
"autoload": {
"psr-0": {
"Book\\": "src/",
"Vehicle\\": "src/"
}
}
psr-4 PSR-4
Looking at vendor/composer/autoload_psr4.php
file you can see the namespaces and the directories that they are mapped to. 查看
vendor/composer/autoload_psr4.php
文件,您可以看到名称空间及其映射到的目录。
composer.json composer.json
"autoload": {
"psr-4": {
"Book\\": "src/",
"Vehicle\\": "src/"
}
}
composer.json composer.json
"autoload": {
"psr-4": {
"Book\\": "src/Book/",
"Vehicle\\": "src/Vehicle/"
}
}
Even when I tried but Composer is a mess. 即使我尝试但是Composer是一团糟。 Sadly, it's the only alternative.of the market.
可悲的是,它是市场的唯一选择。
Why is a mess?. 为什么乱七八糟?
The Composer's autocomplete works fine if you are in control of the code. 如果您控制代码,则Composer的自动完成功能正常。 However, if you are importing a different project, you find yourself with lots of styles and ways to create folders.
但是,如果要导入其他项目,则会发现自己有很多样式和方法来创建文件夹。 For example, some projects are /company/src/class.php while others are company/class.php and others are company/src/class/class.php
例如,一些项目是/company/src/class.php,而其他项目是company / class.php,其他项目是company / src / class / class.php
I created a library that solves it: 我创建了一个解决它的库:
https://github.com/EFTEC/AutoLoadOne (it's free, MIT). https://github.com/EFTEC/AutoLoadOne (它是免费的,麻省理工学院)。
It generates an autoinclude by scanning all the classes of a folder, so it works in every case (psr-0 psr-4, classes without namespace, file with multiple classes.. 它通过扫描文件夹的所有类来生成自动包含,因此它适用于所有情况(psr-0 psr-4,没有命名空间的类,具有多个类的文件..
edit: And again, downvoted without any reason. 编辑:再次,毫无理由地投票。 ;-)
;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.