Chapter 2. PHP style guidelines

Table of Contents

Indentation
Curly braces
Control structures
Arrays
Classes
Functions
Function calls
Operators

The PHP style guidelines are intended to provide a consistent style over all the framework's code. Although they aren't strictly enforced, you might consider following them. We may refuse code temporarily or ask to rewrite it if its style differ too much from the guidelines, in order to keep our code base clean.

Indentation

You must use tabs, with the equivalent of 4 columns per tab.

A tab must always end on a column number multiple of 4, which means that not all tabs necessarily spans 4 columns: they can span over 1, 2 or 3 columns depending on the tab position.

Curly braces

For class and function definitions, you must add a line break before the opening curly brace. For control structures you can write it directly on the structure's line with a space after the closing parenthesis. Closing curly braces must have a line break before them. This means:

<?php

class test
{
	public function curly($iBrace)
	{
		if ($iBrace > 0) {
			echo '>0!';
			return 0;
		} else {
			echo '<=0!';
			somethingHappened('<=0');
			return 1;
		}
	}
}

Control structures

These include if, for, while, switch, etc.

Control structures should have one space between the control keyword and opening parenthesis, to distinguish them from function calls. Curly braces are optional. You don't have to use them if they are not required.

Arrays

Arrays should be formatted with a space separating each element and assignment operator, if applicable:

<?php

$aMyArray = array('hello', 'world', 'nyan' => 'ko');

Note that if the array contains too many items, each element should be broken into its own line, and, unless it would require adding too many tabs, aligned:

<?php

$aMyArray = array(
	'hello'  => 'world',
	'nyan'   => 'ko',
	'pika'   => 'Copyrighted',
	'nupuro' => 'NullPointerException',
);

Note that the last comma is not a typo.

Classes

You must define class members in this order: constants, properties, methods. When inheriting, you must first write the classes inherited and then the interface implemented.

Try to keep everything ordered alphabetically, especially the methods, for easier lookup. When ordering variables alphabetically, you must ignore the first character, as it identifies the type, not the variable name.

Always put the public keyword on public elements to prevent confusion. Similarly, do not use var instead of public for a property.

This would look like this:

<?php

class myClassName // [extends myParentClass[, ...]] [implements myInterface[, ...]]
{
	/**
		DocComment
	*/

	const MY_CONSTANT = 42;

	// more constants...

	/**
		DocComment
	*/

	public /* OR protected OR private */ $iMyInt = 42;

	// more properties...

	/**
		DocComment
	*/

	public /* OR protected OR private [static] */ function myMethod(/* [parameters] */)
	{
		return 42;
	}

	// more methods...
}

Functions

There is no space before the parenthesis when declaring a function.

<?php

function myFunction($sField)
{
	$aSystem['description'] = 'Example of function declaration';
	return $aSystem[$sField];
}

Function calls

There is no space before the parenthesis when calling a function either. Do not put space between the parenthesis and the first and last parameter. Separate each parameter by a comma followed by one space.

<?php

$sVar = webExtend($iWeb, $aExtend);

Operators

There should be one space before and after any operator, with the exception of ++, -- and !.

In the case of a block of related assignments, you may insert tabs to promote readability:

<?php

$iStart			= 3;
$iTheAnswerIs	= (1 + $iStart++) * 9;
$bDoIt			= testAvailability() && !testReadiness();

Note

Tabs are there to improve readability. Too many tabs for no obvious reason (such as aligning all assignment operations over a whole file) reduces readability.