Question

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

Was it helpful?

Solution

For PHP, i'd suggest to follow Zends suggestions

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

OTHER TIPS

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.

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.

You have a number of options:

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

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

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

But like prakash suggests, Zend is a good choice.

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...

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

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

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.)

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.

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 ;-)

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top