Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Created June 28, 2010 14:35
Show Gist options
  • Save Mikulas/455922 to your computer and use it in GitHub Desktop.
Save Mikulas/455922 to your computer and use it in GitHub Desktop.
Coding standard [PHP]
/**
* Coding standard
*
* @copyright Mikuláš Dítě 2010
* @version 1
*/
________________________________________________________________________________
<?php # full php opening tag on first line
# new line after php opening tag
namespace Example; # namespace name in CamelCase
use PackageA\ModelA; # usings ordered alphabetically
use PackageA\ModelC; # - every class except Exceptions must be listed here
use PackageB\ModelB; # - even interfaces and extended class
# two new lines after usings
abstract class FooClass extends BaseClass implements IInterfaceA, IInterfaceB # class name in CamelCase, implements are sorted alphabetically
{ # { is at new line after class header
# new line at class content beginning
const FOO_BAR = 'value'; # constants must be CAPS, words split by underscore
const BAR_FOO = 'value'; # no new line after constant definition
const NUMBER2 = 'value'; # numbers in constant names are not separated by underscore
# three new lines after constants definition
/** @var string description */ # property annotations always online
private $private_a = 'value'; # private comes first, always with underscores
# newline after property definition
/** @var int description */
private $private_b = 1;
# three new lines after properties definition
//protected definitions # protected < private
//public definitions # protected < private
/** # method annotatio always multiline
* @param int
* @param string
* @return int
*/
final public static function get($id, $name) # keywords are sorted alphabetically, comma is always followed by space
{ # { is at new line after function header
if (TRUE) { # space always after keyword (except unset) and arguments, { is at the same line
$e = new Class(); # () must always be put after new
} elseif () { # always continue on same line as } separated by space
// some action # slashes must be separated by space
} elseif (TRUE && TRUE || FALSE && TRUE) { # boolean operators must be separated by spaces
/* another action */ # /* */ must be separated by space
} else { # {} are not optional even for oneline conditions
# last action # # must be separated by space
} # last } must be followed by new line
# suggested optional new line
foreach ($items as $key => $item) { # arrow must be separated by space
}
$variable = NULL; # NULL always in CAPS
$res = array('key' => 'a', 'key2' => 'b', 'foo' => 'c'); # short arrays might be oneline, arrow is separated by spaces
$res = array( # long arrays must be multiline, array( is always followed by newline
'key' => 'a', # indent exactly one tab, line ends with commna, space and newline
'last' => 'b', # last item must be followed by comma
); # ) has same indent as line with array( opening
# newline before return
return FALSE; # boolean always in CAPS
}
} # no newline after last method definition
# exactly one newline at the end of the file, no php closing tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment