duanmei1536 2012-07-27 22:00
浏览 61
已采纳

为什么下划线转换为PSR-0标准中的目录分隔符?

The PSR-0 (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) standard specifies that an underscore in the class name should be converted to a directory separator in the corresponding file name.

To me this does not seem to be a good idea as it creates lots of errors when someone who does not know the standard innocently uses an underscore in the class name and suddenly the autoloader cannot find the class and all sort of weird errors appear (see this stackoverflow issue for example: Symfony2.1 mapping error: class_parents())

So I guess there must be some kind of reason (historical compatibility with some library?) for this "feature". My question is: does anyone know why this was introduced in the PSR-0 standard?

  • 写回答

3条回答 默认 最新

  • drcomwc134525 2012-07-27 22:02
    关注

    Underscores were used in the times that PHP didn't yet support namespaces. A "correct" organized project follows the convention of namespacing the files the same way as the directory structure.

    It is just some general "rule" to organize files in a project.

    So if you have a directory structure of:

    root
      Name
        Package
          MyClass.php
    

    People used to do:

    class Name_Package_MyClass {}
    

    But now that we have namespaces it becomes:

    namespace Name\Package;
    
    class MyClass { }
    

    It is just coding style guideline which ensures that everybody does the same thing.

    So what PSR-0 does is map both the old and the new style of namespacing to a filename.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应