下划线 PSR0 VS PSR4

2013-12-17 18:26:50 +08:00
 raincious
PSR0 —— Autoloading Standard
PSR4 —— Autoloader

按照PSR0里面的约束,命名空间里的下划线(_)必须自动转换成空间分隔符(\);

但是在PSR4里,有描述写到:Underscores have no special meaning in any portion of the fully qualified class name.

PSR4貌似强调的是可以注册不同磁盘位置的库,而不用将库放在一个地方了。

但意思是不是之前PSR0里面说的又不算了?还是我理解错了?但是我看了PSR4的例子代码,没看到转换下划线的部分啊。反倒是PSR0里面写了`str_replace('_', DIRECTORY_SEPARATOR, $className);`这句。

还是说……PHP是一个连标准都不规范的语言……呢?

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
5729 次点击
所在节点    PHP
6 条回复
haiyang416
2013-12-17 18:59:44 +08:00
我没看到 psr0 里对下划线替换带有 must 字样。另外,这规范是自发的,不是官方规定。
raincious
2013-12-17 19:32:19 +08:00
@haiyang416

PSR0: Autoloading Standard
The following describes the ** mandatory ** requirements that ** must be adhered to for autoloader interoperability **.

Mandatory
.....
Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.
.....


PSR4: Underscores have ** no special meaning in any portion of the fully qualified class name **.

如果是真的,感觉这群人就像是在玩。
thbourlove
2013-12-18 15:38:29 +08:00
@raincious

说实话,没搞懂你在纠结些什么。。

PSR是一组规范,没人要求你既要符合 PSR0 又要符合 PSR4。

你可以选择 PSR0 PSR2 这样一个组合作为你的代码规范,同样也可以选择 PSR2 PSR4 这样一个组合。干嘛非要 PSR0 PSR4 这样一个不能满足的组合来作为自己的代码规范呢?少年?

PSR 中只有 PSR2 依赖于 PSR1,PSR1的 namespace 和 classname 依赖于 PSR0。其他各组规范之间是没有什么联系的。最终你要选择出一组适合自己的来用。
raincious
2013-12-18 18:31:11 +08:00
@thbourlove 可能你想的还不够多。

考虑到一个模块的作者守着PSR0命名,另一个守着PSR4的情况了么?

目前看来是不是要避免用下划线了。
thbourlove
2013-12-18 22:33:26 +08:00
@raincious 真没考虑,我想知道那又会怎么样呢亲?
raincious
2013-12-20 08:49:19 +08:00
@thbourlove 好吧,如果你单纯是为了给我送分而进来的……谢谢你。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/93538

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX