-
Notifications
You must be signed in to change notification settings - Fork 37
Coding style
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:
- To unconditionally include a file, use require_once() [WRN.PHP.3.1.1].
- 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].
Required:
When defining a multi-line array, each next line should have the next level of indentation [REQ.PHP.3.3.3].
Required:
- Negative numbers may not be used as indices [REQ.PHP.3.3.1].
- 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
);
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',
);
Required:
- The curly bracket is always placed on the next line, under class name, on a separate line, not indented [REQ.PHP.3.4.1].
- 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.
- The code within a class must have the next level of indentation [REQ.PHP.3.4.3].
- 1 class per 1 PHP file [REQ.PHP.3.4.4].
- 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
{
}
Required:
- All class member variables must be defined in the beginning of the class, before defining any method [REQ.PHP.3.4.5].
- Scope definition is mandtory (protected or public) [REQ.PHP.3.4.6].
- Class member variables with scope private are not allowed [REQ.PHP.3.4.8].
Recommended:
- Class member variables with scope public are not recommended. Better use protected + 2 separate functions getter/setter [WRN.PHP.3.4.2].
- Using the same getter/setter for several variables is not recommended. Instead, use several pairs of individual getter/setter for each variable.
Required:
- Class methods must always define their scope using one of the prefixes, protected or public [REQ.PHP.3.5.1].
- Methods with scope private are not allowed [REQ.PHP.3.5.16].
- Function arguments with default values should be listed at the end of the argument list [REQ.PHP.3.5.3].
- Whenever possible, functions (not methods) must always return a value [REQ.PHP.3.5.4].
- 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].
- Function body begins on the line next to the opening curly bracket or after 1 blank line [REQ.PHP.3.5.6].
- The opening curly bracket must be aligned to keyword function [REQ.PHP.3.5.7].
- 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].
- 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].