且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PSR-0和PSR-4有什么区别?

更新时间:2023-11-16 22:53:04

它们非常相似,所以这并不奇怪,它有点混乱。总结是PSR-0对于PSR-4丢弃的PEAR风格的类名称有一些向后兼容特性,因此它只支持命名空间的代码。在上面的PSR-4不强迫你有整个命名空间作为目录结构,但只有跟随锚点的部分。

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.

例如,如果你定义 Acme\Foo\ 命名空间锚定在 src / 中,PSR-0意味着它会在PSR-4中, Acme\Foo\Bar src / Acme / Foo / Bar.php 将在 src / Bar.php 中找到它,允许更短的目录结构。另一方面,有些人更喜欢有完整的目录结构来清楚地看到什么是命名空间,所以你也可以说 Acme\Foo\ src / Acme / Foo 使用PSR-4,这将使您等同于上述PSR-0行为。

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.

对于新项目和大多数意图和目的的长故事短,你可以使用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.