Which Coding convention to follow for PHP? [closed

2019-01-15 20:13发布

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

9条回答
Deceive 欺骗
2楼-- · 2019-01-15 20:55

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.

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

查看更多
Rolldiameter
3楼-- · 2019-01-15 20:56

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

  • 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 LoggerInterface exposes 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.
查看更多
The star\"
4楼-- · 2019-01-15 20:58

Update: people also use PSR nowadays a lot


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.

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

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.

查看更多
登录 后发表回答