Declaration of a class
When writing a class, you should adhere to the following formatting rules:
-Do not have spaces between the opening parenthesis "(") before the method name and its argument list
-The opening brace "{" is at the end of the statement peer
-The right Brace "}" is on a separate line, aligned with the corresponding declaration statement, unless it is an empty statement, "}" should be immediately after "{"
Class Sample extends Object {
int $ivar 1;
int $ivar 2;
function Sample (int $i, int $j) {
ivar$1 = $i;
ivar$2 = $j;
}
function Emptymethod () {}
...
}
-Null line separation between method and method
Layout
Declare a variable only at the beginning of the code block. (a block refers to any code that is enclosed in the middle of the braces "{" and "}".) Do not declare the variable when it is first used. This can confuse programmers who are distracted, while interfering with the portability of your code within that scope.
function MyMethod () {
int $int 1 = 0; Start of Method block
if ($condition) {
int $int 2 = 0; The start of the "if" block
...
}
}
An exception to this rule is the index variable for the FOR loop
for (int $i = 0; i < $maxLoops; $i + +) {...}
Avoid declaring a local variable that overrides a variable declared at the previous level. For example, do not declare the same variable name in an internal code block:
int $count;
...
function MyMethod () {
if ($condition) {
int $count = 0; Avoid such statements
...
}
...
}