Now that I am starting to use PHP as a programming language I have mashed up a short set of standards that I hope will make it easier for anyone sharing in phpAgile framework development.
These standards are based pretty much on the standards provided for the Fuel framework (see fuelphp.com ).
PHP Files
Short tags
Do not use the PHP short tag option (ie. use <?php ... not <? ... ). Check your PHP.ini file and see the note:
“ Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code be sure not to use short tags ”
File Names
User lower case (with underscores if necessary).
Naming Conventions
Classes
Class names should use title case (with underscores to separate words if necessary).
Methods
Method names should use lower case (with underscores to separate words if necessary)
class Person {
public static function get_where($name, $data)
{
// Some code here
}
}
Variables
Variable names should be concise, and contain only lower case letters and underscores. Loop iterators should be short, preferably a single character.
$forename
$order_total
for ($i = 0; $i < $max; $i++)
Constants
Constants should be all upper case.
MY_CONSTANT
TEMPLATE_PATH
TEXT_DEFAULT
Keywords
Keywords such as true, false, null, as, etc should be all lower case. (also for primitive types like array, integer, string).
SQL keywords should be upper case with lower case table and field names
SELECT id, forename, surname, email, status
FROM person
WHERE status = 'member';
Code formatting
Indentation
Use one tab (not spaces) for each level of nesting, but align code after the indentation with spaces (not tabs).
// indent 1 tab
$membership = 'Affiliate'; // indented with tabs
$status = 'Member'; // aligned using spaces
Control Structures
Code groups (such as if, for, foreach, while, switch) and Function / Method structures should be followed by a space as should parameter/argument lists and values. Braces should be placed on a new line, and break should have the same tab as its case.
if ($arg === true)
{
// do something here
}
elseif ($arg === null)
{
// do something else
}
else
{
// otherwise
}
while ($row = $result->fetch())
{
// loop here
}
switch ($status)
{
case 'value1':
//do something here
break;
default :
//do something here
break;
}
public function get_where ($value, $data)
{
// code here
}
Variables
When initializing variables, one variable should be declared per line. To enhance code readability these should each be on a separate line. Align values and comments when appropriate.
$id = 1234; // do each on its own line
$forename = 'John';
$surname = 'Gallagher';
$membership = 'Full';
Brackets and Parenthesis
No space should come before or after the initial bracket/parenthesis. There should not be a space before closing bracket/parenthesis.