PHP 命名空间是否有完善的命名约定?

发布于 2024-08-12 16:56:24 字数 263 浏览 3 评论 0 原文

到目前为止,我已经看到了许多用于 PHP 命名空间的不同命名约定。有些人使用PascalCase\Just\Like\For\Classes,有些人使用underscored\lower_case\names,有些人甚至使用Java约定的包名称:com\域\项目\包

问题很简单——这些(或其他)约定中的任何一个都可以称为完善的约定吗?为什么?它们中是否有 Zend 等权威机构或知名 PHP 框架的开发人员推荐的?

So far, I've seen many different naming conventions used for PHP namespaces. Some people use PascalCase\Just\Like\For\Classes, some use underscored\lower_case\names, some even use the Java convention for package names: com\domain\project\package.

The question is very simple -- can any of these (or other) conventions be called well-established? Why? Are any of them recommended by authorities like Zend or the developers of well-known PHP frameworks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

摇划花蜜的午后 2024-08-19 16:56:24

由许多不同 PHP 框架和项目的贡献者组成的 PHP 标准工作组针对适当的命名空间使用提出了以下建议:

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

A PHP Standards Working Group made up of contributors to many different PHP frameworks and projects came up with the following proposal for appropriate namespace usage:

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

还给你自由 2024-08-19 16:56:24

我不认为你可以说目前已经有任何成熟的东西,但是有一个 RFC 正在讨论 PEAR2 的问题: http://wiki.php.net/pear/rfc/pear2_naming_standards

我的看法是,命名空间是变量的一种形式,因此应该遵循与它们相同的约定。我通常更喜欢使用 lowercase_underscore 作为自由变量(而不是对象成员),因此最适合我口味的是 namespace\ClassName。这也符合其他语言中最流行的约定。另一方面,对于 5.3 之前的伪命名空间也可以提出相同的论点,但这里事实上的标准似乎使用 CamelCase。

I don't think you can say there is anything well established at this point, but there is an RFC discussing it for PEAR2: http://wiki.php.net/pear/rfc/pear2_naming_standards

My take is that namespaces are a form of variables, and therefore should follow the same convention as they do. I genereally prefer lowercase_underscore for free variables (As opposed to object members), so the most natural for my taste would be namespace\ClassName. This also matches the most prevalent conventions from other languages. On the other hand, the same argument could be made for pre-5.3 pseudo-namespaces, but here the de-facto standard seems to be using CamelCase.

花开半夏魅人心 2024-08-19 16:56:24

就我个人而言,我喜欢用大驼峰式写出类名,用小驼峰式写写类属性和方法,并用下划线作为前缀。

其他局部变量我也在编写不带下划线前缀的小驼峰形式。对象实例总是写成大驼峰式等等。我真的不认为有最好的方法,你只需要与你的编码标准保持一致。这使您可以更快地阅读代码,并且可以更快地了解哪些代码行发生了什么。

Personally I like writing classnames upper camelcase, class attributes and methods lower camelcase and class attributes prefixed with an underscore.

Other local variables i'm also writing lower camelcase without an underscore prefix. Object instances are always written uppper camelcase etc. etc. I don't really think that there is a best way, you just have to be consistent to your codingstandard. This gives you the advantage of reading faster through your code and it should give a faster insight in what's happening at which codelines.

鲜肉鲜肉永远不皱 2024-08-19 16:56:24

有些人可能会说 PascalCase except Acronyms/Initialisms 是首选。因此,例如,以下是合适的版本:

  • “卷曲”
  • 'CurlResponse'
  • “HTTP 状态代码”
  • “网址”
  • 'BTreeMap'(B 树映射)
  • “Id”(标识符)
  • “ID”(身份证明文件)
  • “Char”(角色)
  • “Intl”(国际化)
  • “雷达”(无线电探测和测距)

来源:wiki.php.net


扩展编码标准,以明确指定在编写用户级类名时如何处理缩写词和首字母缩略词/缩写词。目前的规则是:

<块引用>

应该为类指定描述性名称。避免使用缩写
如有可能。类名中的每个单词都应以大写开头
字母,不带下划线分隔符(以大写字母开头的 CamelCaps
信)。类名应以“父级”的名称为前缀
set'(例如扩展名)...

来源:位于github.com< /p>

虽然声明应避免使用缩写,但没有提及如果使用缩写该怎么办;特别是在缩写词/首字母缩写词的情况下。现在基本上有三种可能的选择:

  1. PascalCase except Acronyms/Initialisms - 这是大多数用户级类名的编写方式,并且它与
    许多其他编程语言;

  2. Always PascalCase - 这基本上就是PSR-1 定义了,但是,它会使大多数当前存在的用户级类名称
    无效;

  3. Do Nothing - 这当然自动意味着允许任何方法,并且围绕此主题的社区讨论将
    继续。

...

“我们应该使用什么类命名风格?” (最终结果):

  • Pascal 大小写(首字母缩略词除外):15
  • 始终为 Pascal 大小写:11

来源:位于 wiki.php.net


PSR-1

  • 命名空间和类名

    ...

    类名必须在 StudlyCaps 中声明。

    ...

  • 来源:在 php-fig.org

    Some might say that PascalCase except Acronyms/Initialisms is the preferred choice. So, for example, the following are appropriate versions:

    • 'Curl'
    • 'CurlResponse'
    • 'HTTPStatusCode'
    • 'URL'
    • 'BTreeMap' (B-tree Map)
    • 'Id' (Identifier)
    • 'ID' (Identity Document)
    • 'Char' (Character)
    • 'Intl' (Internationalization)
    • 'Radar' (Radio Detecting and Ranging)

    Source: At wiki.php.net


    Extend the coding standard to explicitly specify how abbreviations and acronyms/initialisms are to be handled when writing user-level class names. The current rule is:

    Classes should be given descriptive names. Avoid using abbreviations
    where possible. Each word in the class name should start with a capital
    letter, without underscore delimiters (CamelCaps starting with a capital
    letter). The class name should be prefixed with the name of the 'parent
    set' (e.g. the name of the extension)...

    Source: At github.com

    While it is stated that abbreviations should be avoided, it is silent on what to do if they are used; especially in the case of acronyms/initialisms. There are essentially three choices possible now:

    1. PascalCase except Acronyms/Initialisms - Which is how the majority of user-level class names are written, and it matches the approach of
      many other programming languages;

    2. Always PascalCase - Which is basically what PSR-1 defines, however, it would make most of the currently existing user-level class names
      invalid;

    3. Do Nothing - Which of course automatically means that any approach is allowed, and the community discussions around this topic will
      continue.

    ...

    "What class naming style should we use?" (final result):

    • PascalCase except Acronyms: 15
    • Always PascalCase: 11

    Source: At wiki.php.net


    PSR-1

    1. Namespace and Class Names

      ...

      Class names MUST be declared in StudlyCaps.

      ...

    Source: At php-fig.org

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文