-
Notifications
You must be signed in to change notification settings - Fork 0
Coding conventions
crystal88 edited this page Aug 20, 2012
·
1 revision
- Use under_score naming instead of camelCalse $my_var = 0; //correct $myVar = 0; //incorrect
- Use tabs for intendation, no spaces.
- Opening brackets should be on the same line separated with exactly one space. Closing brackets should be on their own line
foreach (...) { //statements here } </pre>
- Every statement block should be on brackets, even if it's only one statement. Exception is if that statement breaks the execution of the surrounding block.
foreach ($arr as $itm) { $sum += $itm; }
if ($x === TRUE) { $y = FALSE; } if ($x === TRUE) break; if ($x === TRUE) return; if ($x === TRUE) throw new Exception(...); if ($x === TRUE) { //incorrect, braces are not needed here continue; }
- Don't put spaces before and after a condition, expect it begins with "!". In this case you should put a space before and after the "!".
if ($x === TRUE) { //correct //... }
if ( $x === TRUE ) { //incorrect //... } if ( ! $x) { //correct // ... } while (!$x) { //incorrect // ... }
- Use boolean and null constants in uppercase
$x = TRUE; //correct $x = true; //incorrect $x = NULL; //correct
- When comparing against a constant value, the constant should be the first operator
if (NULL == $x) { //correct //... }
if ($x == NULL) { //incorrect //... }
- always use class type hints where possible. Never use the
array
type hint since this forbids you to passArrayObject
instances as arguments.function fn(MyClass $obj) { //correct //... }
function fn2(array $arr) { //incorrect //... }
- Try not to write lines longer than 80 characters.
- prefer using
isset()
overarray_key_exists()
(note that they don't work exactly the same way)
- Every line of a multi-line expression should start with an operator or comma:
if ($expression_1 && $expression_2) { //correct //... }
if ($expression_1 && //incorrect $expression_2) { //... }
$obj->method1() //correct ->method2();
$obj->method1()-> //incorrect method2();
$obj->method($arrrrrrrrrrg1 //correct , $arg2);
$obj->method($arrrrrrrrrrg1, //incorrect $arg2);
- local variables which are used to temporaly store a function or method result value should be named
$rval
or$result
.function sum($arr) { $rval = 0; foreach ($arr as $itm) { $rval += $itm; } return $rval; }
- Class attributes should be separated with an empty line. Always use
public
access modifier for public members.public $attr1; //correct
var $attr2; //incorrect
public $attr3; public $attr4; //incorrect, missing empty line
public $attr5; //the empty line is needed here too
/** * @var string $attr6 */ public $attr6;
function mymethod() { //incorrect, missing public modifier //... }
- The name of private and protected attributes should start with an underscore. Public attribue names should never start with an underscore.
protected $_attr1; //correct
protected $attr2; //incorrect
public $attr3; //correct
public $_attr4; //incorrect
- Don't write empty brackets when calling parameterless constructors
$o = new MyClass; //correct $o = new MyClass(); //incorrect
- class naming conventions:
- all classes must be in a namespace
- all namespaces should be in lowercase
- all classes of a library must be in a separate namespace under thecyclone
namespace. A library can have at most 1 class out of its own namespace, and this class must be in thecyclone
namespace (example: all classes of the DB library are in thecyclone\db
namespace, except thecyclone\DB
class).
- the name of a class is recommended to have a meaningful name on its own too, without its namespace name.