PSR-0和PSR-4有什么区别? [英] What is the difference between PSR-0 and PSR-4?

查看:368
本文介绍了PSR-0和PSR-4有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读过有关名称空间以及它们如何受益的信息。

I've recently read about namespaces and how they are beneficial.

我正在Laravel中创建一个项目,并尝试从类映射自动加载到命名空间。

I'm creating a project in Laravel and was trying to move from class map autoloading to namespacing.

我似乎不能理解PSR-0和PSR-4之间的实际差别。
我读过的一些资源是

I can't seem to grasp what the actual difference is between PSR-0 and PSR-4. Some resources that I've read from are

  • Battle of the Autoloaders
  • Laracasts PSR-4 autoloading
  • PSR-0
  • PSR-4

我了解:


  • PSR-4不会将下划线转换为目录分隔符

  • 某些特定的作曲者规则导致目录结构变得复杂,这反过来使得PSR-0命名空间冗长,因此创建了PSR-

解释差异的示例将不胜感激。

Examples explaining the difference would be appreciated.

推荐答案

它们非常相似,所以这并不奇怪,它有点混乱。总结是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.

这篇关于PSR-0和PSR-4有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆