Skip to content
QuestionDevelopment edited this page Apr 8, 2016 · 5 revisions

##PHP

  • Files MUST use only <?php tags. ((do not use <? or <?= ))
  • Files MUST use only UTF-8 without BOM for PHP code.
  • The closing ?> tag MUST be omitted from files containing only PHP.
  • All PHP files MUST end with a single blank line.

##Classes

  • Class names MUST be declared in underscore naming convention (example_class).
  • Method names MUST be declared in underscore naming convention (example_method).
  • Class constants MUST be declared in all upper case with underscore separators.
  • In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
  • Namespaces must be declared for every class and should follow the following format vendor\model;
  • Class file names must be match class name exactly

##Naming Conventions

  • File names, variables names should all utilize underscore naming convention (file_name.php)
  • The PHP constants true, false, and null MUST be in lower case.
  • Acronyms must be all lowercase (id = correct) (Id or ID = incorrect)
  • Preference always goes to 'title' over 'name' except with the value is actually a name (ie input field or first name of a person)
  • Abbreviations should not be utilized

##Comments

  • All files should have comments that are parsable by phpDoc
  • For syntax questions please refer to their documenation
  • Every file requires an file comment which should have the following
    • Title
    • Description
    • Package
    • Subpackage
    • Author
    • Version
    • Edit Log
    • Todo list (if applies)
  • Every method requires the following
    • Description
    • List of parameters
    • Description of what returns
  • If it is not completely obvious what you are doing there should be inline comments

##Plugins

  • Each plugin must have all data within one folder
  • Each plug is required to have an init.php file.
  • Plugin names must be underscore naming convention

##PHP Formatting

  • Code MUST use 4 spaces for indenting, not tabs
  • Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
  • Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body.
  • Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.
namespace vendor\package;

class ClassName
{
    public function foo($arg1, &$arg2, $arg3 = [])
    {
        // method body
    }
}
  • An if structure looks like the following. Note the placement of parentheses, spaces, and braces; and that else and elseif are on the same line as the closing brace from the earlier body.
if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body;
}
  • A switch structure looks like the following. Note the placement of parentheses, spaces, and braces. The case statement MUST be indented once from switch, and the break keyword (or other terminating keyword) MUST be indented at the same level as the case body. There MUST be a comment when fall-through is intentional in a non-empty case body.
switch ($expr) {
    case 0:
        echo 'First case, with a break';
        break;
    case 1:
        echo 'Second case, which falls through';
        // no break
    case 2:
    case 3:
    case 4:
        echo 'Third case, return instead of break';
        return;
    default:
        echo 'Default case';
        break;
}
  • A while and do while statement looks like the following. Note the placement of parentheses, spaces, and braces.
while ($expr) {
    // structure body
} do {
    // structure body;
} while ($expr);
  • A for statement looks like the following. Note the placement of parentheses, spaces, and braces
for ($i = 0; $i < 10; $i++) {
    // for body
}
  • A foreach statement looks like the following. Note the placement of parentheses, spaces, and braces.
foreach ($iterable as $key => $value) {
    // foreach body
}
  • A try catch block looks like the following. Note the placement of parentheses, spaces, and braces.
try {
    // try body
} catch (FirstExceptionType $e) {
    // catch body
} catch (OtherExceptionType $e) {
    // catch body
}
Clone this wiki locally