Skip to content
zenmiu edited this page Aug 4, 2011 · 3 revisions

Including code

Required:

Operators include, require, include_once and require_once are not functions, thus the paths to the included files are to be enclosed in brackets [REQ.PHP.3.1.1] [!].

Recommended:

  1. To unconditionally include a file, use require_once() [WRN.PHP.3.1.1].
  2. To include a file that can be included or not included depending on its availability, use include_once(). Exception - the magic function __autoload and similar structures; include them using require_once [WRN.PHP.3.1.2].

Arrays

Required:

When defining a multi-line array, each next line should have the next level of indentation [REQ.PHP.3.3.3].

Numerically indexed arrays

Required:

  1. Negative numbers may not be used as indices [REQ.PHP.3.3.1].
  2. An array indexing always begins with 0.

Recommended:

When defining a multi-line array, it is recommended to have the same number of elements on each line, except the last one.

Example:

$sampleArray = array(
    1, 2, 3, 'Zend', 'Studio',
    $a, $b, $c, $d, $e
    56.44, $x, 500
);

Associative arrays

Required:

When defining a multi-line associative array, do not place more than one key-value pair per line [REQ.PHP.3.3.4].

$sampleArray = array(
    'firstKey'  => 'firstValue',
    'secondKey' => 'secondValue',
);

Classes

Defining a class

Required:

  1. The curly bracket is always placed on the next line, under class name, on a separate line, not indented [REQ.PHP.3.4.1].
  2. If the length of the line with a class naming block exceeds 120 characters, it is carried over to a new line at the word extends or implements, indented by 1 position.
  3. The code within a class must have the next level of indentation [REQ.PHP.3.4.3].
  4. 1 class per 1 PHP file [REQ.PHP.3.4.4].
  5. Strictly observe the following order of methods and properties: abstract public -> abstract protected -> static public -> static protected -> static private -> public -> protected -> private. The order is to be observed in either the entire class, if it is not split into blocks, or in each block [REQ.PHP.3.4.7].

Recommended:

Placing additional code in a class file is not recommended [WRN.PHP.3.4.1].

Example:

/**
 * Doc block here
 */
class Sample_Class
{
    // the content of the class must be
    // indented by four spaces
}

class Class1
    extends Class0
    implements Interface0
{
}

Class member variables

Required:

  1. All class member variables must be defined in the beginning of the class, before defining any method [REQ.PHP.3.4.5].
  2. Scope definition is mandtory (protected or public) [REQ.PHP.3.4.6].
  3. Class member variables with scope private are not allowed [REQ.PHP.3.4.8].

Recommended:

  1. Class member variables with scope public are not recommended. Better use protected + 2 separate functions getter/setter [WRN.PHP.3.4.2].
  2. Using the same getter/setter for several variables is not recommended. Instead, use several pairs of individual getter/setter for each variable.

Functions and methods (definition)

Required:

  1. Class methods must always define their scope using one of the prefixes, protected or public [REQ.PHP.3.5.1].
  2. Methods with scope private are not allowed [REQ.PHP.3.5.16].
  3. Function arguments with default values should be listed at the end of the argument list [REQ.PHP.3.5.3].
  4. Whenever possible, functions (not methods) must always return a value [REQ.PHP.3.5.4].
  5. The opening curly bracket should be placed on a new line if the function argument declaration block consists of a single-line [REQ.PHP.3.5.5].
  6. Function body begins on the line next to the opening curly bracket or after 1 blank line [REQ.PHP.3.5.6].
  7. The opening curly bracket must be aligned to keyword function [REQ.PHP.3.5.7].
  8. If the argument declaration block of a function consists of multiple lines: a. The line with the function name must be terminated after the opening bracket [REQ.PHP.3.5.8]. b. The closing bracket must be on a new line [REQ.PHP.3.5.9]. c. The closing bracket must be aligned to keyword function [REQ.PHP.3.5.10]. d. The argument must have the next level of indentation [REQ.PHP.3.5.11]. e. The opening curly bracket must follow the closing bracket of the argument block, separated with a space [REQ.PHP.3.5.12].
  9. Arguments with default values must be separated from the default values by a sequence of characters space + equality sign + space [REQ.PHP.3.5.14];

Recommended:

Functions in the global scope or within scope of other functions are highly discouraged [WRN.PHP.3.5.1].

Clone this wiki locally