PSR-0 和 PSR-4 有什么区别? [英] What Are the Differences Between PSR-0 and PSR-4?

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

问题描述

最近我读到了关于命名空间以及它们的好处的文章.我目前正在 Laravel 中创建一个项目,并尝试从类映射自动加载转移到命名空间.但是,我似乎无法理解 PSR-0 和 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. However, I can't seem to grasp what the actual difference is between PSR-0 and PSR-4.

我读过的一些资源是……

我的理解:

  • PSR-4 不会将下划线转换为目录分隔符
  • composer 的某些特定规则会导致目录结构变得复杂,从而使 PSR-0 命名空间变得冗长,因此创建了 PSR-4

将不胜感激提供解释差异的示例.

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.

例如,如果您定义 AcmeFoo 命名空间锚定在 src/ 中,使用 PSR-0 意味着它将查找 Acmesrc/Acme/Foo/Bar.php 中的 FooBar 而在 PSR-4 中它将在 src/Bar.php 中查找它,允许更短的目录结构.另一方面,有些人更喜欢拥有完整的目录结构以清楚地查看哪个命名空间中的内容,因此您也可以说 AcmeFoosrc/Acme/Foo 与 PSR-4 将提供与上述 PSR-0 行为等效的行为.

For example if you define that the AcmeFoo namespace is anchored in src/, with PSR-0 it means it will look for AcmeFooBar 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 AcmeFoo 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天全站免登陆