PHP

Coding Standards in PHP

I feel about this very strongly. At least about some particular topics.
It is very important for every group project (even for a one man project – you never know when sobody might join you) to have some common guidelines how to write code.
Some projects don’t seem to have them at all. Others don’t care.
And I often see the weirdest combinations of standards – even in some bigger projects.

Quite a while ago I already published a complete summary (see the link below).
This time I want to go into details what PHP is concerned.

I will only cover the main aspects – the most important standards.
The full list can be found here.

It’s the single most annoying thing to clean up somebody else’s code which is messed up with the wrong LFs, indent signs, spaces etc.
So I published guidelines that make the most sense for my projects.

Indentation

Some projects use spaces. Absolutely wrong. Spaces are not supposed to be used this way. You can use them to separate sentences, even conditions like ($x && $y). But to indent code there is only one correct solution called "tabs". One tab for each level of indentation.

The nice side-effect: A good IDE can transform this into pseudo-spaces, allowing the user to decide how much width a single tab has. Some like more, some less indentation length per level.
Either way the code is easily transportable/editable cross-browser and cross-operationsystem.

Tip: This can easily be applied globally using my IndentShell

Curly Braces

This can really be a pain in the … sometimes. As with most of the important php projects they MUST be opened in the same line. It increases readability (less lines on the monitor) without a single disadvantage.
Therefore I can’t and I won’t understand why somebody would do it otherwise.

if (condition1 || condition2) {
    action1;
} elseif (condition3 && (condition4 || condition5)) {
    action2;
} else {
    defaultaction;
}

Additionally, they must also be used in situations where they are technically optional (due to the inline format they only add a single line of code, but again increase readability + make it easy to add code).

Spaces

After commas one space, before curly opening brackets one space and between operaters one space:

# wrong (6 spaces missing)
if(($var1)||($var2)){
    $res=array_merge($defaultOptions,$options);
}
# correct
if (($var1) || ($var2)) {
    $res = array_merge($defaultOptions, $options);
}

if/else/elseif

Those should always include {} – even if is a one-liner. And elseif should be used instead of else if.
@see are-elseif-and-else-if-completely-synonymous for a discussion on that.

Comments

Some discourage the usage of the shell style comment sign (#). I don’t think this is justified.
A lot of code – especially in developing process – has commented out code fragments. At some point you don’t even know anymore what is temporary commented out and what is a real comment.

# apply style (REAL COMMENT)
$this->apply($data);
/*
$this->isCommentedOutForSomeReason();
*/
...
// blabla
//$this->add('oh, this too, is commented out!');
...
# Another important comment here!?
$this->Model->setRecursive(1);
...

So if a programmer uses // for single lines and /* */ for multiple lines to comment them out, he can easily use # for actual comments. This way real comments are distinguishable from commented out stuff more easily.

Closing Tags in PHP

Files that contain only PHP code should not include the closing tag ?> as it can lead to header problems with the HTTP response if the text editor you’re using automatically adds a new line to the end of your files.

Tip: Those can easily be removed using my PhpTagShell

Automation

If you want to automate CS checks, use phpcs. There are CakePHP sniff rules for the core. I use my own codesniffer with a few more features available – and to comply with my coding standards that are built on top of the core ones.

Vista

If everyone used the same coding standards (especially tabs for indent, inline spaces and some other common guidelines) we wouldn’t have any trouble adapting foreign code or code by other programmers into the own repository.
Nobody would ever write HTML like <sPaN cLaSs='foo'>...</SPan> – so why not trying to establish some common ground rules on PHP?

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.