1. PHP code must always be delimited by the full-form, standard PHP tags. Short tags are only allowed within view scripts.
For example :
[codesyntax lang=”php”]
<?php
// Php code here
?>
[/codesyntax]
[codesyntax lang=”php”]
// index.phtml
<?= 'hello' ?>
[/codesyntax]
2. When a string is literal (contains no variable substitutions), the apostrophe or "single quote" must always used to demarcate the string.
For example :
[codesyntax lang=”php”]
$a = 'Example String';
$b = `Excute Something`;
[/codesyntax]
3. 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.
For example :
[codesyntax lang=”php”]
$sql = "SELECT `id`, `name` from `people` WHERE `name`='Fred' OR `name`='Susan'";
[/codesyntax]
4. Variable substitution is permitted only in the way that : $+variable name.
For example :
[codesyntax lang=”php”]
$greeting = "Hello $name, welcome back!"; // permitted
$greeting = "Hello {$name}, welcome back!"; // permitted
$greeting = "Hello ${name}, welcome back!"; // not permitted
[/codesyntax]
5. Strings may be concatenated using the "." operator. A space must always be added before and after the "." operator to improve readability.
For example :
$company = 'Zend' . 'Technologies';
6. 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.
For example :
[codesyntax lang=”php”]
$sql = "SELECT `id`, `name` FROM `people` "
. "WHERE `name` = 'Susan' "
. "ORDER BY `name` ASC ";
[/codesyntax]
7. Negative numbers are not permitted as array indices. An indexed array may be started with any non-negative number, however this is discouraged and it is recommended that all arrays have a base index of 0.
For example :
[codesyntax lang=”php”]
$sampleArray = array(-1 => -1, 0 => 0); // wrong
$sampleArray = array(0 => -1, 1 => 0); // right
$sampleArray = array(1 => -1, 2 => 0); // right
[/codesyntax]
8. When declaring indexed arrays with the array construct, a trailing space must be added after each comma delimiter to improve readability.
For example :
[codesyntax lang=”php”]
$sampleArray = array(1, 2, 3, 'Zend', 'Studio');
[/codesyntax]
9. It is also permitted to declare multi-line indexed arrays using the array construct. In this case, each successive line must be padded with spaces such that beginning of each line and each value is aligned.
For example :
$sampleArray = array(1, 2, 3, 'Zend',
'Studio', $a, $b, $c,
56.44, $d, 500);
10. When declaring associative arrays with the array construct, it is encouraged to break the statement into multiple lines. In this case, each successive line must be padded with whitespace such that both the keys and the values are aligned.
For example :
$sampleArray = array('firstKey' => 'firstValue',
'secondKey' => 'secondValue');
11. The brace is always written on the line underneath the class name ("one true brace" form).
For example :
class foo
{
// one true brace
}
class foo {
// wrong
}
12. Every class must have a documentation block that conforms to the phpDocumentor standard.
For example :
/**
* Class Docblock Here
*/
class Zend_Class
{
}
13. Any code within a class must be indented the standard indent of four spaces.
For example :
class Zend_Class
{
$spaces = '4 spaces';
if ($spaces == '4 spaces') {
echo 'is permitted !';
}
}
class Zend_Class
{
$spaces = 'less then 4 spaces';
if ($spaces != '4 spaces') {
echo 'is not permitted !';
}
}
14. Only one class is permitted per PHP file. Placing additional code in a class file is permitted but heavily discouraged. In these files, a blank line must separate the class from any additional PHP code in the file.
For example :
<?php
// It's permitted but discouraged
class Zend_Class
{
static $foo = 'foo';
}
echo Zend_Class::$foo;
<?php
/**
* It's not permitted to
* declare more than one class
* in one php file
*/
class Class_One
{
}
class Class_Two
{
}
?>
15. Any variables declared in a class must be listed at the top of the class, prior to declaring any functions.
For example :
class right
{
public $foo = 'before function declaring';
public function fun()
{
}
}
class wrong
{
public function fun()
{
}
public $foo = 'after function declaring';
}
16. 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 methods having the set and get prefixes.
For example :
class foo
{
var $unpermitted = "It's unpermitted!";
private $_privateVariable = "It's private";
protected $_protectedVariable = "It's protected";
public $publicVariable = "It's public";
public function setPrivateVariable($value)
{
$this->_privateVariable = $value;
}
public function getPrivateVariable()
(
return $this->_privateVariable;
)
}
$foo = new foo();
// Discouraged
echo $foo->publicVariable;
// Encouraged
echo $foo->getPrivateVariable();
17. Methods must always declare their visibility by using one of the private, protected, or public constructs.
For example :
class foo
{
function goo()
{
// It's not permitted
}
private function bar()
{
// It's right
}
protected function baz()
{
// It's right
}
public function zoo()
{
// It's right
}
}
18. Following the more common usage in the PHP developer community, static methods should declare their visibility first.
For example :
class foo
{
static function baz()
{
// It's not permitted
}
public static function bar()
{
// It's right
}
}
19. As for classes, the opening brace for a function or method is always written on the line underneath the function or method name ("one true brace" form).
For example :
class foo
{
public function braceIsUnderneath()
{
// It's right
}
public function braceIsNotUnderneath() {
// It's wrong
}
}
20. There is no space between the function or method name and the opening parenthesis for the arguments.
For example :
class foo
{
public function someSpacesAfterMe ($a)
{
// It's wrong
}
public function noSpacesAfterMe($a)
{
// It's right
}
}
21. Passing function or method arguments by reference is only permitted by defining the reference in the function or method declaration. Call-time pass by-reference is prohibited.
For example :
function defineRefInMethod(&$a)
{
$a = 'a';
}
function callTimePassRef($a)
{
$a = 'a';
}
$b = 'b';
$c = 'c';
// It's permitted
defineRefInMethod($b);
echo $b; // 'a'
// It's prohibited
callTimePassRef(&$c);
echo $c; // 'a'
22. The return value must not be enclosed in parentheses. This can hinder readability and can also break code if a function or method is later changed to return by reference.
For example :
class foo
{
public $bar = 'bar';
public function goo()
{
return ($this->bar);
}
public function & zoo()
{
return ($this->bar);
}
}
$foo = new foo();
// Seems no problem
echo $foo->goo();
/**
* One notice will raise saying that :
* "Only variable references should be returned by reference"
*/
echo $foo->zoo();
23. The use of type hinting is encouraged where possible with respect to the component design.
For example :
class Zend_Component
{
public function foo(SomeInterface $object)
{}
public function bar(array $options)
{}
}
24. Where possible, try to keep your use of exceptions vs. type hinting consistent, and not mix both approaches at the same time in the same method for validating argument types.
For example :
class Not_Zend_Class
{
}
class foo
{
public function bar(Zend_Class $zc)
{
}
public function goo($zc)
{
if (!$zc instanceof Zend_Class) {
throw new Exception('$zc is not instance of Zend_Class');
}
}
}
$foo = new foo();
$zc = new Not_Zend_Class();
/**
* Catchable fatal error raised saying that :
* "Argument 1 passed to foo::bar() must be an instance of Zend_Class,
* instance of Not_Zend_Class given"
*/
$foo->bar($zc);
/**
* Message saying : '$zc is not instance of Zend_Class'
*/
try {
$foo->goo($zc);
} catch (Exception $e) {
echo $e->getMessage();
}
25. Function arguments are separated by a single trailing space after the comma delimiter.
For example :
// Nice
threeArguments(1, 2, 3);
// Bad
threeArguments(1,2,3);
26. For functions whose arguments permit 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.
For example :
threeArguments(array(1, 2, 3), 2, 3);
threeArguments(array(1, 2, 3, 'Zend',
'Studio', $a, $b, $c,
56.44, $d, 500), 2, 3);
27. Control statements based on the "if", "else", and "else if" constructs must have a single space before the opening parenthesis of the conditional, and a single space between the closing parenthesis and opening brace.
For example :
// It's right
if ($spaceOutSide) {
// ...
} else if ($spaceOutSide) {
// ...
} else {
// ...
}
// It's wrong
if($noSpaceOutSide){
// ...
}else if($noSpaceOutSide){
// ...
}else{
// ...
}
28. 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.
For example :
if (($a != 2) and ($b == 1)) {
$a = $b;
}
29. 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 four spaces.
For example :
// It's right
if ($braceSameLine) {
echo 'Nice : indented 4 spaces';
}
// It's wrong
if ($braceNotSameLine)
{
echo 'Bad : indented 2 spaces';
}
30. PHP allows for these statements to be written without braces in some circumstances. The coding standard makes no differentiation and all "if", "else if", or "else" statements must use braces.
Example of formatted statement :
// It's right to with braces
if ($a != 2) {
$a = 2;
} else if ($a == 3) {
$a = 4;
} else {
$a = 7;
}
// It's wrong to without braces
if ($a != 2)
$a = 2;
else if ($a == 3)
$a = 4;
else
$a = 7;
31. Use of the "elseif" construct is not allowed in favor of the "else if" combination.
For example :
if (true) {
//
} else if {
// right
}
if (true) {
//
} elseif {
// wrong
}
32. 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 between the closing parenthesis and the opening brace.
For example :
switch ($num) {
// right
}
switch($num){
// wrong
}
33. All content within the "switch" statement must be indented four spaces. Content under each "case" statement must be indented an additional four spaces.
For example :
switch ($indentedSpaces) {
case 2:
echo "It's wrong";
break;
case 4:
echo "It's right";
break;
default:
break;
}
34. The construct "default" may never be omitted from a "switch" statement.
For example :
switch ($isWithDefault) {
case false:
break;
default:
echo "It's right to always with 'default'";
break;
}
switch ($isWithDefault) {
case false:
echo "It's wrong to without 'default'";
break;
}
35. It is sometimes useful to write a "case" statement which falls through to the next case by not including a "break" or "return". To distinguish these cases from bugs, such "case" statements must contain the comment "// break intentionally omitted".
For example :
switch ($numPeople) {
case 1: // break intentionally omitted
case 2:
break;
default:
break;
}
36. Usage of the global keyword is not allowed. Use $GLOBALS[xxx] instead.
For example :
$a = 'a';
function foo()
{
// It's wrong
global $a;
$foo = $a;
// It's right
$foo = $GLOBALS['a'];
return $foo;
}
echo foo();