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].

Using constants instead of 'magic numbers'

Required:

Any number involved in an expression must be predefined as a constant.

Explanation:

‘Magic number’ is some unclear number used in the source code. It is ‘magic’ because nobody has an idea of what it means (including the author of the code 3 months later).

Example:

if     (22 == $foo) { start_nuclear_war(); }
elseif (19 == $foo) { return_wazoo_bucks(); }
elseif (16 == $foo) { infinite_loop(); }
else                { im_lost_help(); }

What do the 19 and 22 mean in this example? How are you supposed to track the error if one number is swapped for the other? The widespread use of ‘magic numbers’ is a clear sign of a beginner. This programmer has never worked in a team and never supported the code (his or someone else’s); otherwise, he would have never written this way. Use real names instead of ‘magic numbers’. For example, you can take advantage of define().

Example:

define("PRESIDENT_HAS_GONE_CRAZY", "22");
define("WE_SCREWED_UP", "19");
define("THEY_DIDNOT_PAY", "16");

if     (PRESIDENT_HAS_GONE_CRAZY == $foo) { start_nuclear_war(); }
elseif (WE_SCREWED_UP            == $foo) { return_wazoo_bucks(); }
elseif (THEY_DIDNOT_PAY          == $foo) { infinite_loop(); }
else                                      { wow_great_i_know_why_im_here(); }

Global variables

Required:

Using global variables is not allowed [REQ.PHP.3.7.1].

Unary operators (++ / --)

Required:

Unary operators must be called on a separate line of code [REQ.PHP.3.9.1].

$foo[$i++] = $j; // incorrect

$foo[--$j] = $i; // incorrect

$foo[$i] = $j;
$i++;            // correct

$j--;
$foo[$j] = $i;   // correct

Checking availability of variables

Required:

If a variable may be unavailable, it must be initialized before it is used.

Example:

if (isset($_POST['x'])) {
    $x = $_POST['x'];

} else {
    $x = '';
}

or

$x = isset($_POST['x']) ? $_POST['x'] : '';

Restrictions on using function calls

Required:

  1. Using eval() is not allowed [REQ.PHP.3.11.1].
  2. Using create_function() is not allowed [REQ.PHP.3.11.2].
  3. Using functions php_version(), php_uname(), php_sapi_name() is not allowed. Use constants instead [REQ.PHP.3.18.1].
Clone this wiki locally