V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
raincious
V2EX  ›  PHP

下划线 PSR0 VS PSR4

  •  
  •   raincious · 2013-12-17 18:26:50 +08:00 · 5704 次点击
    这是一个创建于 3789 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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
    6 条回复    1970-01-01 08:00:00 +08:00
    haiyang416
        1
    haiyang416  
       2013-12-17 18:59:44 +08:00 via Android
    我没看到 psr0 里对下划线替换带有 must 字样。另外,这规范是自发的,不是官方规定。
    raincious
        2
    raincious  
    OP
       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
        3
    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
        4
    raincious  
    OP
       2013-12-18 18:31:11 +08:00
    @thbourlove 可能你想的还不够多。

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

    目前看来是不是要避免用下划线了。
    thbourlove
        5
    thbourlove  
       2013-12-18 22:33:26 +08:00
    @raincious 真没考虑,我想知道那又会怎么样呢亲?
    raincious
        6
    raincious  
    OP
       2013-12-20 08:49:19 +08:00
    @thbourlove 好吧,如果你单纯是为了给我送分而进来的……谢谢你。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2222 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:01 · PVG 18:01 · LAX 03:01 · JFK 06:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.