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

查看:82
本文介绍了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.

我已阅读的一些资源...

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

我的理解:

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

我们将举例说明不同之处.

Examples explaining the difference would be appreciated.

推荐答案

它们非常相似,因此有点混乱也就不足为奇了.总结是,对于PSR-4放弃的PEAR样式类名,PSR-0具有一些向后兼容功能,因此它仅支持命名空间代码.最重要的是,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意味着它将在src/Acme/Foo/Bar.php中寻找Acme\Foo\Bar,而在PSR-4中它将在src/Acme/Foo/Bar.php中寻找它. c4>,允许使用较短的目录结构.另一方面,有些人更喜欢具有完整的目录结构来清楚地了解哪个命名空间,因此您还可以说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天全站免登陆