BigBoardroom’s PHP Coding Standards

We are very proud of our code here at BigBoardroom, and I’ve taken some time to re-type up our PHP coding standards in case other developers might want a standards document to help with their own code. As our standards update over time, I will come back here to update this page.

Coding Standards for PHP

– Indentation

  1. Use tab to indent, not 4 spaces.

– Functions and Methods

  1. Function names may only contain alphanumeric characters. Underscores are not permitted.
  2. Numbers are permitted in function names but are discouraged.
  3. Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized (camelCaps).
  4. Verbosity is encouraged. Function names should be as verbose as is practical to enhance the understandability of code.
  5. These are examples of acceptable names for functions:
    • filterInput()
    • getElementById()
    • widgetFactory()
  6. For object-oriented programming, accessors for objects should always be prefixed with either “get” or “set”.
  7. When using design patterns, such as the singleton or factory patterns, the name of the method should contain the pattern name where practical to make the pattern more readily recognizable.
  8. Functions in the global scope (“floating functions”) are permitted but discouraged. It is recommended that these functions should be wrapped in a static class.

– Variables

  1. Variable names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in variable names but are discouraged.
  2. For class member variables that are declared with the “private” or “protected” construct, the first character of the variable name must be a single underscore. This is the only acceptable usage of an underscore in a function name. Member variables declared “public” may never start with an underscore or contain an underscore.
  3. Like function names, variable names must always start with a lowercase letter and follow the “camelCaps” capitalization convention.
  4. Verbosity is encouraged. Variables should always be as verbose as practical. Terse variable names such as “$i” and “$n” are discouraged for anything other than the smallest loop contexts. If a loop contains more than 20 lines of code, the variables for the indices need to have more descriptive names.

– Constants

  1. Constants may contain both alphanumeric characters and the underscore. Numbers are permitted in constant names.
  2. Constants must always have all letters capitalized.
  3. To enhance readablity, words in constant names must be separated by underscore characters. For example, EMBED_SUPPRESS_EMBED_EXCEPTION is permitted but EMBED_SUPPRESSEMBEDEXCEPTION is not.
  4. Constants must be defined as class members by using the “const” construct. Defining constants in
    the global scope with “define” is permitted but discouraged.

– PHP Code Demarcation

  1. PHP code must always be delimited by the full-form, standard PHP tags:
    <?php
    ?>
  2. Short tags are never allowed. For files containing only PHP code, the closing tag must always be omitted.

– Strings

  1. When a string is literal (contains no variable substitutions), the apostrophe or “single quote” must always used to demarcate the string.
    Example:
    $a = 'Example String';

– String Literals Containing Apostrophes

  1. When a literal string itself contains apostrophes, it is permitted to demarcate the string with quotation marks or “double quotes”. This is especially encouraged for SQL statements:
    $sql = “SELECT `id`, `name` from `people` WHERE `name`=’Fred’ OR `name`=’Susan'”;
    The above syntax is preferred over escaping apostrophes.

– Variable Substitution

  1. Variable substitution is permitted using either of these two forms:
    $greeting = "Hello ".$name.", welcome back!";
    $greeting = 'Hello '.$name.', welcome back!';

– String Concatenation

  1. Strings may be concatenated using the “.” operator. A space must always be added before and after the “.” operator to improve readability:
    $company = 'BigBoardroom' . 'Rocks';
  2. When concatenating strings with the “.” operator, it is permitted to break the statement into multiple lines to improve readability. In these cases, each successive line should be padded with whitespace such that the “.” operator is aligned under the “=” operator:
    $sql = "SELECT `id`, `name` FROM `people` "
    . "WHERE `name` = 'Susan' "
    . "ORDER BY `name` ASC ";

– Numerically Indexed Arrays

  1. As a general rule of thumb, arrays declarations using the array construct should have each value on a new line:
    $sampleArray = array(
        1,
        2,
        3,
        'Zend',
        'Studio'
    );

– Associative Arrays

  1. The same for associative arrays, each key => value pair should be on a new line:
    $sampleArray = array(
       'firstKey' => 'firstValue',
       'secondKey' => 'secondValue'
    );

– Class Declaration

  1. Classes must be named by following the naming conventions.
  2. The brace is always written on the line underneath the class name (“one true brace” form).
  3. Every class must have a documentation block that conforms to the PHPDocumentor standard.
  4. Any code within a class must be indented.
  5. Only one class is permitted per PHP file.
  6. Placing additional code in a class file is permitted but discouraged. In these files, two blank lines must separate the class from any additional PHP code in the file.
  7. This is an example of an acceptable class declaration:

    /**
    * Documentation Block Here
    */
    class SampleClass
    {
       // entire content of class
       // must be indented
    }

– Class Member Variables

  1. Member variables must be named by following the variable naming conventions.
  2. Any variables declared in a class must be listed at the top of the class, prior to declaring any functions.
  3. In PHP5, the var construct is not permitted. Member variables always declare their visibility by using one of the private, protected, or public constructs. Accessing member variables directly by making them public is permitted but discouraged in favor of accessor variables (set/get).

– Function and Method Declaration

  1. Functions must be named by following the naming conventions.
  2. Functions inside classes must always declare their visibility by using one of the private, protected, or public constructs.
  3. Like classes, the brace is always written on the line underneath the function name (“one true brace” form).
  4. There is no space between the function name and the opening parenthesis for the arguments.
  5. Functions in the global scope are strongly discouraged.
  6. This is an example of an acceptable function declaration in a class:

    /**
    * Documentation Block Here
    */
    class Foo
    {
       /**
       * Documentation Block Here
       */

       public function bar()
       {
           // entire content of function
           // must be indented.
       }
    }

  7. NOTE: Passing by-reference is permitted in the function declaration only:

    /**
    * Documentation Block Here
    */
    class Foo
    {
       /**
       * Documentation Block Here
       */
       public function bar(&$baz)
       {}
    }
  8. Call-time pass by-reference is prohibited.
  9. The return value must not be enclosed in parentheses. This can hinder readability and can also break code if a method is later changed to return by reference.

    /**
    * Documentation Block Here
    */
    class Foo
    {
       /**
       * WRONG
       */
       public function bar()
       {
          return($this->bar);
       }

       /**
       * RIGHT
       */
       public function bar()
       {
          return $this->bar;
       }
    }

– Function and Method Usage

  1. Function arguments are separated by a single trailing space after the comma delimiter. This is an example of an acceptable function call for a function that takes three arguments:
    function threeArguments(1, 2, 3);
  2. Call-time pass by-reference is prohibited. See the function declarations section for the proper way to pass function arguments by-reference.
  3. For functions whose arguments permitted arrays, the function call may include the “array” construct and can be split into multiple lines to improve readability. In these cases, the standards for writing arrays still apply:

    threeArguments(array(1, 2, 3), 2, 3);
    threeArguments(array(1, 2, 3, 'Zend', 'Studio',
    $a, $b, $c,
    56.44, $d, 500), 2, 3);

– If / Else / Elseif

  1. Control statements based on the if and else if constructs must have a single space before the opening parenthesis of the conditional, and a single space after the closing parenthesis.
  2. Within the conditional statements between the parentheses, operators must be separated by spaces for readability. Inner parentheses are encouraged to improve logical grouping of larger conditionals.
  3. The opening brace is written on the same line as the conditional statement. The closing brace is always written on its own line. Any content within the braces must be indented spaces.
  4. Example:

    if ($a != 2) {
       $a = 2;
    }
  5. For “if” statements that include “else if” or “else”, the formatting must be as in these examples:

    if ($a != 2) {
       $a = 2;
    } else {
       $a = 7;
    }

    if ($a != 2) {
       $a = 2;
    } else if ($a == 3) {
       $a = 4;
    } else {
       $a = 7;
    }

– Switch

  1. Control statements written with the “switch” construct must have a single space before the opening parenthesis of the conditional statement, and also a single space after the closing parenthesis.
  2. All content within the “switch” statement must be indented. Content under each “case” statement must be indented an additional four spaces.
  3. Example:

    switch ($numPeople) {
       case 1:
          break;
       case 2:
          break;
       default:
          break;
    }
  4. The construct default may never be omitted from a switch statement.
Comments are closed.