Based on PSR-0 to PSR-2
As our server uses PHP 5.3, use of shorthand operators are not available. This document only covers the simplest coding standards that should be used by any developers to make their code base readable and help future developers understand what the code is trying to do.
Proposed Coding Standards
- Defining Filename
- The filename of the php file should always explain briefly what would be its purpose.
- If the file contains a class, the name of the Class should be the same as the file.
- Always end with a single blank line.
- Closing tag must be omitted from files containing only PHP.
- Defining Class Names, Method Names, and Constants.
- Files must use only <?php tags
- Class names must be declared in StudlyCaps
- Class constants must be declared in all upper case with underscore operators.
- Method names must be declared in camelCase
- Defining a Class Structure
- Class name should always have three space after the opening tag.
- Method names are always camelCase()
- Method names should briefly discuss what their function. Ex: multiplyToTen($num)
- Method arguments should be in one line, inside the Method. Ex: camelCase($arg1, $arg2=””)
- If having multiple method arguments, each arguments should be separated by space.
- Method braces should have their respective line.
- Keywords and variables should be always in lowercase. Ex: $num, $text, $array
- Code must use an indent of 4 spaces and must not use tabs for indenting.
- Comments are recommended and is mandatory in every function that requires deep understanding of the algorithm used.
- Conditional Statements
-
If Else statements should look like this in a PHP file,
if ($expr1) { } elseif ($expr2) { } else { }
-
Foreach statements should look like this in a PHP file,
foreach ($array as $key => $value) { }
-
For loop statements should look like this in a PHP file,
for ($i = 0; $i < 10; $i++) { }
-
Try Catch statements should look like this in a PHP file,
try { } catch (FirstExceptionType $e) { }