forked from opencart/opencart
-
Notifications
You must be signed in to change notification settings - Fork 0
Coding standards
James Allsup edited this page Jan 2, 2014
·
3 revisions
- File types & encoding
- PHP tags
- Indentation
- Spacing
- Whitespace
- New lines
- File naming
- Class & method naming
- Helper naming
- PHP variable naming
- User defined constants
- PHP constants
All view/template files have the extension .tpl
### PHP Tags Short PHP opening tags and ASP tags are not supported. The characters should be lowercase. ``` All PHP files must include a closing tag for versions before 2.0. PHP files in and after 2.0 will no longer have a closing tag.
?>
### Indentation PHP files must be indented using the TAB character. 4 space tabs are not supported.
HTML in template files (.tpl) must be indented using 2 spaces, not 4 spaces or TABS. JavaScript must be indented using the TAB character.
### Spacing IF, WHILE, FOR etc should have a space before and after the brackets.
if () {
if(){
ELSE etc should have a space after and before the curly braces
} else {
}else{
Type casting does NOT have a space before the variable
(int)$var
(int) $var
Setting a variable should always have a space before and after the equals sign
$var = 1;
$var=1;
### Whitespace After any code, but before a new line - there should be no white space. The same is true for an empty line.
After the closing PHP tag it is extremely important to remove any white space.
### New Lines
Opening curly braces do not go onto a new line, they will always have a space before and be on the same line.
1 True Brace Style (1TBS) (WIKI)
if ($my_example == 1) {
class ModelExampleExample extends Model {
public function addExample() {
} else {
if ($my_example == 1)
{
class ModelExampleExample extends Model
{
public function addExample()
{
}
else
{
### File naming All files should be in lower case and words separated by an underscore.
### Class & method naming Class names and method names should be camel case.
class ModelExampleExample extends Model
public function addExample()
class model_exampleexample extends Model
public function add_example()
A method scope should always be cast.
public function addExample()
function addExample()
### PHP Function (helpers) naming Helper function names should be lower case and an underscore used to separate words.
### PHP variable naming PHP variables should be lower case and an underscore used to separate words.
$var = 123;
$new_var = 12345;
$Var = 123;
$newVar = 12345;
### User defined constants User defined constants are set as upper case.
define('MY_VAR', 'My constant string value');
define('my_var', 'My constant string value');
### PHP constants These types of constant (true,false,null) are set as lower case
$my_var = true;
$my_var = TRUE;