Variable names
- The first letter of a variable must start with lowercase
- Variables native or arrays must be separated by underscore(_) every word change: $my_var
- The variables that are Objects each word end must be capitalized, example: $myVar
- Global variables must be all capitalized, example: $MY_VAR
Assignments
- There must be a space between the variable and the operators:
$my_var = 17; $a = $b;
Operators
- "+", "-", "*", "/", "=" and any combination of them (eg "/ =") need a space between
members of the left and right$a +
17
;
$result = $b /
2
;
$i + =
34
;
- «.» no space between the left and right limbs
echo $a.$b;
$c = $d.$
Este
-> foo ();
Recommendation
For performance reasons, do not exceed the use of concatenation.
- ". =" Needs a space between the left and right members
$a. =
'Debug'
;
Declarations
- if, elseif, while, for: must have a space between the if keyword and the parentheses
if
( )
while
( )
- When a combination of if and else else is used and if both must offer a value, the else must be avoided.
if
( )
return
false
;
return
true
;
Recommendation
We recommend one result per method / function
- When a method / function offers a boolean value and the current value of the method / function depends on it, the if statement should be avoided
public
aFirstMethod ()
{
return
$
Este
-> aSecondMethod ();
}
- The tests should be grouped by "entity"
if
($price AND! Empty ($price))
[...]
if
(! Validate :: $myObject OR $myObject-> id === NULL)
[...]
Visibility
- Visibility must be defined at all times, even when it is a public method.
- The order of the method properties must be: visibility static function name ()
private
static
function foo ()
Method / Function Names
- Method and function names always start with a lowercase character and each of the following words must start with an uppercase character (CamelCase)
public
function myExempleMethodWithALotOfWordsInItsName ()
- The brackets that are presented in the method code have to be preceded by a return
public
function myMethod ($arg1, $arg2)
{
[...]
}
- Los nombres de los métodos y las funciones deben ser nombres explícitos, por lo tanto nombres de función como «b()» o «ef()» están completamente prohibidos.
Excepciones
The only exceptions are the translation function called "l ()" and the debugging functions "p ()", "d ()".
Enumeration
Commas must be followed (and only followed) by a space.
protected function myProtectedMethod ($arg1, $arg2, $arg3 = null ) |
Objects / Classes
- The name of the object must be singular
class
Customer
- The class name must follow the CamelCase model, except that the first letter is capitalized
class
MyBeautifulClass
Definitions
- Definitions names must be entered in uppercase
- The names of the definitions must have the prefix "PS_" inside the kernel and the module
define (
'PS_DEBUG'
,
1
);
define (
'PS_MODULE_NAME_DEBUG'
,
1
);
- Definition names do not allow alphabetic characters. Except "_".
Keywords
All keywords must be lowercase
ex. as, case, if, echo, null
Constants
Constants must be in uppercase, except "true" and "false" and "null", which must be in lowercase
ex. "ENT_NOQUOTE", "true"
Configuration variables
Configuration variables follow the same rules as definitions
Chains
Strings must be enclosed in single quotes, never double
threw out 'Debug' ;
|
Comments
- Within functions and methods, only the comment "//" tag is allowed
- After the comment tag «//», a space "// Comment" is required
// My great comment
- The comment tag "//" is allowed at the end of a line of code
$a =
17
+
23
;
// A comment inside my exemple function
- External functions and methods, only the «/" and "/ »Comments are allowed
/ * This method is required for compatibility issues * /
public
function foo ()
{
// Some code explanation right here
[...]
}
- Comment PHP Doc Element is required before method declarations
/**
* Return field value if possible (both classical and multilingual fields)
*
* Case 1: Return value if present in $_POST / $_GET
* Case 2: Return object value
*
* @param object $obj Object
* @param string $key Field name
* @param integer $id_lang Language id (optional)
* @return string
*/
protected
function getFieldValue ($obj, $key, $id_lang = NULL)
For more information
For more information about the PHP Doc rule: see this useful link
Return of values
- Return declarations do not need parentheses, except for a compound expression
return
$result;
return
($a + $b);
return
(a () - b ());
return
true
;
- Disarm a function
return
;
Call
The calling function preceded by an "@" is prohibited but be careful when calling the function / method with login / password or argument path.
myfunction ()
|
Labels
- A blank line should be left after the PHP opening tag
<?php
require_once (
'my_file.inc.php'
);
- Final PHP tag is forbidden
Indentation
- El carácter de tabulación («t») es el único caracter de indentación que se permite
- Each level of indentation must be represented by a single tab character
function foo ($a)
{
if
($a ==
null
)
return
false
;
[...]
}
Matrix
- The array keyword must not be followed by a space
array (
17
,
23
,
42
);
- The indentation should be as follows when a lot of data is inside an array:
$a = array (
36
=> $b,
$c =>
'foo'
,
$d => array (
17
,
23
,
42
),
$e => array (
0
=>
'zero'
,
1
=> $one
)
);
Block
Square brackets are prohibited when defining only one statement or statement combination
if (! $result)
|
No Comment