PHP 应遵循哪种编码约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/139427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:38:41  来源:igfitidea点击:

Which Coding convention to follow for PHP?

phpcoding-style

提问by Igor Drincic

Should I stick with Sun's Java code conventions for PHP code?

对于 PHP 代码,我应该坚持 Sun 的 Java 代码约定吗?

回答by Justin

You should be following one of the PSR standards for PHP approved by the Framework Interop Group

您应该遵循Framework Interop Group批准的 PHP PSR 标准之一

  • PSR-0- Aims to provide a standard file, class and namespace convention..
  • PSR-1- Aims to ensure a high level of technical interoperability between shared PHP code.
  • PSR-2- Provides a Coding Style Guide for projects looking to standardise their code.
  • PSR-3- Describes a common interface for logging libraries; the LoggerInterfaceexposes eight methods to write logs to the eight RFC 5424 levels.
  • PSR-4- Describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0.
  • PSR-0- 旨在提供标准文件、类和命名空间约定。
  • PSR-1- 旨在确保共享 PHP 代码之间的高水平技术互操作性。
  • PSR-2- 为希望标准化其代码的项目提供编码风格指南。
  • PSR-3- 描述日志库的通用接口;在LoggerInterface自曝八种方法来写日志八倍RFC 5424的水平。
  • PSR-4- 描述从文件路径自动加载类的规范。它是完全可互操作的,并且可以与任何其他自动加载规范(包括 PSR-0)一起使用。

回答by prakash

For PHP, i'd suggest to follow Zends suggestions

对于 PHP,我建议遵循Zends 建议

As you might know, Zend is the most widely used framework!

您可能知道,Zend 是使用最广泛的框架!

回答by Gordon

Update:people also use PSRnowadays a lot

更新:现在人们也经常使用PSR



Zend Framework and PEAR standards are pretty much the most common coding conventions. If your company adopted another one, stick to the your company's convention though. Better than having no convention at all. And they only work if everyone sticks to them.

Zend Framework 和 PEAR 标准几乎是最常见的编码约定。如果您的公司采用了另一个,请坚持您公司的惯例。总比没有约定好。而且只有在每个人都坚持的情况下,它们才会奏效。

Also see:

另见:

Basically, all of the major frameworks have a coding convention somewhere in their documentation. The official (but mostly unknown IMO) PHP Coding Guidelines can be found at

基本上,所有主要框架的文档中都有一个编码约定。可以在以下位置找到官方(但大部分未知 IMO)PHP 编码指南

If you need to validate code against a coding convention, consider using CodeSniffer. Some IDEs also offer automatic sourcecode formatting by templates. For instance Zend Studio has the ZF coding guidelines built-in, so it's just a click to format code to that convention.

如果您需要根据编码约定验证代码,请考虑使用CodeSniffer。一些 IDE 还通过模板提供自动源代码格式。例如,Zend Studio 内置了 ZF 编码指南,因此只需单击即可将代码格式化为该约定。

回答by Gordon

You have a number of options:

您有多种选择:

Zend: http://framework.zend.com/manual/en/coding-standard.html

Zend:http: //framework.zend.com/manual/en/coding-standard.html

Pear: http://pear.php.net/manual/en/standards.php

梨:http: //pear.php.net/manual/en/standards.php

Wordpress: http://codex.wordpress.org/WordPress_Coding_Standards

Wordpress:http: //codex.wordpress.org/WordPress_Coding_Standards

But like prakash suggests, Zend is a good choice.

但就像 prakash 建议的那样,Zend 是一个不错的选择。

回答by Patrick Desjardins

If you are in a business follow the business code convention.

如果您从事业务,请遵循业务代码约定。

If it's for a personal project you can get the specific language specification (if you do Java than Java, if you do Php than PHP). If it's your personal project you can change few things if you desire...

如果是个人项目可以拿到具体的语言规范(如果你做Java而不是Java,如果你做Php而不是PHP)。如果这是您的个人项目,您可以根据需要更改一些内容...

If you do open source project, you should go see what's already in place.

如果你做开源项目,你应该去看看什么已经到位。

回答by John Parker

As Gordon says, the Zendand PEAR standards are the effective industry standard.

正如 Gordon 所说,Zend和 PEAR 标准是有效的行业标准。

However, the company's code quite possibly pre-dates these so depending on the size of the code base there may be little value in investing the time to make the move to one of these. (That said, if they ever want to use static code analysis tools you could possibly use this as an impetus to seriously consider moving to Zend, etc.)

然而,该公司的代码很可能早于这些,因此根据代码库的大小,投入时间转向其中之一可能没有什么价值。(也就是说,如果他们想使用静态代码分析工具,您可以将其用作认真考虑转向 Zend 等的动力。)

However, being realistic, as long as they have a sensible standard that they stick to there's no real issue here - you'll find yourself adjusting how you "see" the code accordingly.

然而,现实一点,只要他们有一个合理的标准,他们就不会在这里出现真正的问题——你会发现自己相应地调整了你“看到”代码的方式。

回答by Adam

There are pros and cons to any coding style. I spend a lot of time working with code from many sources doing integrations so sometimes end up seeing many different styles in a single day (different naming conventions, braces placement, tabs vs spaces etc)

任何编码风格都有利有弊。我花了很多时间处理来自许多来源的代码进行集成,所以有时最终会在一天内看到许多不同的样式(不同的命名约定、大括号放置、制表符与空格等)

As far as I'm concerned - the most important thing if you are working with existing code is to follow the style of the code that you are editing. If you don't you make things harder for anyone following after you.

就我而言 - 如果您正在使用现有代码,最重要的事情就是遵循您正在编辑的代码的风格。如果你不这样做,你会让追随你的人变得更难。

If you are writing new code than you should have freedom to do it the way that makes you most efficient.

如果您正在编写新代码,那么您应该可以自由地以最高效的方式进行编写。

I find that company coding guidelines are often far to detailed and end up being forgotten after a few years and a bit of churn in the software team ;-)

我发现公司的编码指南往往不够详细,最终会在几年后被遗忘,并且在软件团队中出现了一些变动;-)

回答by George Marian

Coding styles vary between groups and it isn't a one size fits all type of thing. The most important thing is having a standard that's followed consistently and not going overboard. Too many rules can be just as bad as not enough.

编码风格因组而异,它不是一种适合所有类型的东西。最重要的是有一个一贯遵循的标准,而不是过火。太多的规则可能与不够一样糟糕。

I used to prefer the K&R style (the second one). After having to adjust to the Allman style (your preference), I now feel that it makes code more readable and have changed my preference.

我曾经更喜欢 K&R 风格(第二个)。在不得不适应 Allman 风格(您的偏好)之后,我现在觉得它使代码更具可读性并且改变了我的偏好。

This Wikipedia articleis a decent place to start. It also includes a link to the PEAR Coding Standards, among others.

这篇维基百科文章是一个不错的起点。它还包括指向 PEAR 编码标准等的链接。

回答by Treb

There are many different coding conventions out there. Have a look at what other people use (read some example code and see how easy it is to understand what is being done) and take your pick.

有许多不同的编码约定。看看其他人使用什么(阅读一些示例代码,看看理解正在做什么是多么容易)并选择你。

The important part is to choose one and stick to it.

重要的是选择一个并坚持下去。