php PSR-0 和 PSR-4 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24868586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What Are the Differences Between PSR-0 and PSR-4?
提问by Varun Nath
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. However, I can't seem to grasp what the actual difference is between PSR-0 and PSR-4.
最近我阅读了关于命名空间以及它们是如何有益的。我目前正在 Laravel 中创建一个项目,并试图从类映射自动加载转移到命名空间。但是,我似乎无法理解 PSR-0 和 PSR-4 之间的实际区别。
Some resources that I've read are...
我读过的一些资源是...
What I understand:
我的理解:
- PSR-4 does not convert underscores to directory separators
- Certain specific rules of composer cause the directory structure to become complex which in turn makes PSR-0 namespacing verbose and thus PSR-4 was created
- PSR-4 不会将下划线转换为目录分隔符
- composer 的某些特定规则导致目录结构变得复杂,从而使 PSR-0 命名空间变得冗长,因此创建了 PSR-4
Examples explaining the difference would be appreciated.
解释差异的示例将不胜感激。
回答by Seldaek
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. 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-0 对 PSR-4 丢弃的 PEAR 样式类名具有一些向后兼容性功能,因此它仅支持命名空间代码。最重要的是,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. 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/
,与PSR-0这意味着它会寻找Acme\Foo\Bar
在src/Acme/Foo/Bar.php
同时PSR-4将寻找它src/Bar.php
,允许较短的目录结构。另一方面,有些人更喜欢使用完整的目录结构来清楚地看到哪个命名空间中的内容,因此您也可以说这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。
回答by Adil Abbasi
Here are the major differences,
这是主要的区别,
1.For example if you define that the Acme\Foo\
namespace is anchored in src/
,
1.例如,如果您定义Acme\Foo\
命名空间锚定在src/
,
- with PSR-0 it means it will look for
Acme\Foo\Bar
insrc/Acme/Foo/Bar.php
- while in PSR-4 it will look for
Acme\Foo\Bar
insrc/Bar.php(where Bar class is)
.
- 使用 PSR-0 意味着它将
Acme\Foo\Bar
在src/Acme/Foo/Bar.php
- 而在 PSR-4 中,它将
Acme\Foo\Bar
在src/Bar.php(where Bar class is)
.
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 workAcme\Foo\Bar
--->src/Acme/Foo/Bar2.php
(for Bar class) will not work
Acme\Foo\Bar
--->src/Acme/Foo/Bar.php
(对于 Bar 类)将起作用Acme\Foo\Bar
--->src/Acme/Foo/Bar2.php
(对于 Bar 类)将不起作用
回答by wbswjc
PSR-4 is something like 'relative path', PSR-0, 'absolute path'.
PSR-4 类似于“相对路径”、PSR-0、“绝对路径”。
e.g.
例如
config:
配置:
'App\Controller' => 'dir/'
PSR-0autoload:
PSR-0自动加载:
App\Controller\IndexController --> dir/App/Controller/IndexController.php
PSR-4autoload:
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/
回答by Udhav Sarvaiya
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. Below is an example of the folder structure:
通常,您将在根文件夹中创建一个 src/ 目录,与 vendor/ 位于同一级别,然后在那里添加您的项目。以下是文件夹结构的示例:
.
+-- 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
作曲家.json
"autoload": {
"psr-0": {
"Book\": "src/",
"Vehicle\": "src/"
}
}
- Looking for Book\History\UnitedStates in src/Book/History/UnitedStates.php
- Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php
- 在src/Book/History/UnitedStates.php 中寻找Book\History\UnitedStates
- 在src/Vehicle/Air/Wings/Airplane.php 中寻找Vehicle\Air\Wings\Airplane
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
作曲家.json
"autoload": {
"psr-4": {
"Book\": "src/",
"Vehicle\": "src/"
}
}
- Looking for Book\History\UnitedStates in src/History/UnitedStates.php
- Looking for Vehicle\Air\Wings\Airplane in src/Air/Wings/Airplane.php
- 在src/History/UnitedStates.php 中寻找Book\History\UnitedStates
- 在src/Air/Wings/Airplane.php 中寻找Vehicle\Air\Wings\Airplane
composer.json
作曲家.json
"autoload": {
"psr-4": {
"Book\": "src/Book/",
"Vehicle\": "src/Vehicle/"
}
}
- Looking for Book\History\UnitedStates src/Book/History/UnitedStates.php
- Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php
- 寻找Book\History\UnitedStates src/Book/History/UnitedStates.php
- 在src/Vehicle/Air/Wings/Airplane.php 中寻找Vehicle\Air\Wings\Airplane
回答by magallanes
Even when I tried but Composer is a mess. 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. 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
即使我尝试过 Composer 也是一团糟。可悲的是,这是市场的唯一选择。
为什么乱七八糟?。
如果您可以控制代码,则 Composer 的自动完成功能可以正常工作。但是,如果您要导入不同的项目,您会发现自己有许多创建文件夹的样式和方法。例如,有些项目是/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. ;-)
编辑:再次,无缘无故地投了反对票。;-)