POLL: How do you format your php?

One of the problems that are immediately faced is that as the tab size changes, the code indenting becomes inconsistent.

for example:


$car        = new Vehicle('car');
$boat       = new Vehicle('boat');
$motorcycle = new Vehicle('motorcycle');

It looks fine with a tabstop of 4, but with a tabstop of 8, the two tabs in the gap suddenly become:


$car                = new Vehicle('car');
$boat               = new Vehicle('boat');
$motorcycle = new Vehicle('motorcycle');

So what do you do? Demand that tab stops all be the same size, such as 4 spaces? Where then is the benefit of tabs being able to represent multiple numbers of spaces?

Smarter editors that use spaces when you tab seem to be more common-place these days.

Exactly, this is why tabs are banned in a lot of different coding guidelines (including my own)!

Depends how lazy Im feeling and on how complex the script is ;). But I prefer A to B

Same with commenting the code, if its a basic script then very little comment, if theres a lot going on then Ill comment more than I code, I dont wanna spend a day in 2 years time trying to figure out what the hell I did.

I’ve been using 5.3 before it even was released. It is still PHP there is nothing dramatically different other then a few additions. It is not like upgrading to a new or different Operating System.

It’s on 5.3.5 now… been out quite a long time!

Right, but how stable is it? I’m always wary about upgrading server software in a production environment… and 1 year is not “quite a long time” in relative terms… if there’s a security hole, and you’re building PCI compliant apps, you’re screwed.

There’s a reason managed servers all come pre-installed with 5.2 still…

If there is a security hole it is probably (more then likely) in the previous version as well.
You also say that like the previous version has no (or would not have) security holes itself.

And it is as stable as PHP has ever been, take it as you will.

[ot]PHP 5.3 has been out nearly 2 years, not one. Further, PHP 5.2.x has had it’s final release put out and will not receive new updates unless they are critical security flaws.

PHP 5.4 is on the horizon - it MIGHT be out this fall, but predicting PHP releases is almost as bad as picking lottery numbers.[/ot]

[ot]

Package managers are so slow to get the latest updates for PHP in. Really annoys me. Have to install apache and PHP from source to get it to work with 5.3, but at least it’s done now!

Two years is easily enough time to find out if there are any major show stoppers preventing you from upgrading. I see no reason not to really.[/ot]

Sounds good to me… thanks! :slight_smile:

I’m with Zalucius and ScallioXTX (style known as ANSI or Allman)

Style B is known as Whitesmiths.

A is known as K&R or 1TBS - the One True Brace Style.

Allman has the opening braces on a new line, but not indented.

Not quite. K&R and 1TBS are where classes and functions have braces on a new line, while control statements keep it on the same line.


function func()
{
    if (condition) {
        ...
    }
}

K&R allows optional braces on control statements.


if (condition)
    func();

1TBS makes braces mandatory.


if (condition) [color="green"]{[/color]
    func();
[color="green"]}[/color]

Yeh, which is what a lot of people seem to do. I use 1TBS but with functions and classes following the same rules as everything else.

Same here, function, class or switch I put it on a new line, everything else gets it inline.

class Demo {
  function __construct() {
    echo 'Opening brace on same line.';
  }
}

In PHP I type opening brace on the same line, after a space. That’s what I learned when I read my first C++ book and it has stuck with me since then. Neat indenting makes this very clean and easy to read in my opinion. I’m currently reading the book “Clean Code: A Handbook of Agile Software Craftsmanship” which I really recommend for both intermediate and advanced coders fine-tuning their habit of writing clean code.

Recently I’ve been using Ruby though so I haven’t had to care much about braces at all.

But let’s not forget what’s most important of all for writing clean code; consistency. You, and your team, should settle for a coding style and go with it during the entire project. I’ve seen projects where different programmers use different styles and it isn’t pretty.

I favour style “A”, or rather a variation of 1TBS as mentioned earlier. Like @stormrider, I style classes and functions the same as other control structures.

However, with “A” the 2nd to last closing brace should be indented…

function name() { 
   function last() { 
      // Do something 
   } 
}

The closing brace always matches a control statement… function, if, while, etc.

I used to code in Delphi as well, although I have never mimicked the positioning of BEGIN…END with the opening/closing braces {…} as in Allmans style - I think because the opening brace on its own always looked like a waste of space to me and by including the opening brace on the same line you could see more code without having to scroll.

I actually find style “B” where you have indented the braces as well (Whitesmiths style) a bit confusing. If I was to give the opening brace its own line I wouldn’t indent it, keeping it inline with the control structure (which is more like true 1TBS) …

function name() 
{ 
   function last() 
   { 
      //Do something 
   } 
}

opening brace on a new line PLEASE people!!

Is that a personal preference, or a style guide of some particular name that you require to be followed?

Can’t you see the urgency? This is how it simply must be done! PLEASE PAUL!