PHP Coding Specification
In order to make our code more readable and maintainable, and to write less comments, we want to achieve the purpose of "code as Document" by naming the specification and writing the code. All PHP files in the wing-high e-commerce system must follow the coding specifications specified in this document. At the same time, we have added a specification check on the SVN server side, such as code that does not conform to the specification in the submitted code, and SVN returns an error message. Please read the SVN error prompt with a detailed description of the error file, number of lines, and non-conforming specifications.
For third-party class libraries, please contact the project manager if necessary.
Editor settings
- Code indentation: All indents use spaces instead of tab tabs, and each indent contains 4 spaces.
- File encoding: All PHP files are saved as UTF-8 ( no BOM ) character encoding, ANSI is not allowed.
Naming conventions
Variable naming
The variable name is named after the hump, the first letter is lowercase, and the first letter of the word is capitalized. For example:
$currentUser is correct, but $currentuser and $current _user are not correct.
The $REQUESTURL is correct, but $requestURL is not correct.
The name should be descriptive and concise. We naturally don't want to use lengthy sentences as variable names, but it's better to enter a few characters than to wonder what a variable is for.
Constant naming
Constant naming is made up of all uppercase letters and underscores (_), such as Order_type.
Loop counter
The only case where a single character variable name is allowed is when it is used as a loop counter. In this case, the counter of the outer loop should always be $i. If there is a loop inside the loop, its counter should be $j, then $k, and so on. This specification does not apply if the counter of the loop is a variable that already exists and has a meaningful name.
for ($i = 0; $i < $outerSize; $i + +) {for ($j = 0; $j < $innerSize; $j + +) { foo ($i, $j); } }
Function name
Functions should also be named in a descriptive manner. Here we are not programming with C, we do not want to write a function such as "stristr ()" such as this. As above, the words are separated by the hump way. It is preferable to have a verb somewhere in the function name. Good function names such as Printloginstatus (), Getuserdata (), and so on.
function parameters
Parameters follow the same conventions as variable names. We don't want a bunch of these functions: Dostuff ($a, $b, $c). In most cases, we want to just look at the declaration of a function and know how to use it. The number of parameters needs to be controlled within 5 parameters, and more than 5 parameters can be passed using an array.
Class
- name of Class : First of all, it must be a meaningful English word or phrase. The first letter of the class name is capitalized, and the first letter of each word also needs to be capitalized.
- member variables of a class: The naming of member variables and the naming conventions for variables are consistent.
- member method of the class : The name of the method and the function naming are consistent. It is important to note that for private methods, we require that the method name must begin with an underscore.
Summarize
The basic philosophy here is not to be lazy and hurt the clarity of the code. However, this balance must be mastered by some common sense, for example, Printloginstatusforagivenuser () has gone overboard-the function is named Printuserloginstatus () better, or just Printloginstatus ().
Code layout
File header
In addition to the template files and third-party PHP programs, all PHP file headers must contain a standard file header to describe the purpose of the file and some other information. Such as:
?
Please refer to the instructions in PHP documention for the copyright and other tags.
The wording of the annotation
According to the popular notation we require that the comments for the code snippet be written in the following way:
Block-level annotations:
This annotation is used to describe the purpose of the following lines, generally using a blank line to distinguish a block of code, as follows
/* Here is the content of the comment *//** * If the content of the comment is more than * please write it this way * *?
In-line comments:
This comment is used to describe the code for a change, and generally the comment and code are kept on the same line, such as:
$userInfo = $user->getinfo (); Get detailed information about your users?
Curly Braces!!!
The position of the braces, we have a lot of controversy, in fact, for all kinds of arguments have their own truth, but there is a point basically everyone is consistent, this is: always use curly braces , no matter how short the statement, always use curly braces. For example:
/* These are the right */ if (condition) { dostuff (); } while (condition) { dostuff (); } for ($i = 0; $i < size; $i + +) { dostuff (); }
?
For the position of curly braces, that is, the braces for classes, functions, and control statements are always on the same line . For example:
Class MyClass {public function foo () { if (cond) { dosomething ();}} }?
Use spaces before and after symbols
This is another easy and easy step to keep the code readable without too much effort. Whenever you write an assignment, an expression, and so on, always leave a space between the symbols. Basically, write the code as English. Insert a space between the variable name and the operator. Do not add a space after the starting bracket or before terminating the brackets. Do not add spaces before commas or semicolons. Some examples illustrate this very well. For example:
/* Each pair gives the wrong way to follow the right way. */ $i =0; $i = 0; if ($i <7) ... if ($i < 7) ... if (($i < 7) && ($j > 8)) ... if (($i < 7) && ($j > 8)) ... Do_stuff ($i, "foo", $b); Do_stuff ($i, "foo", $b); for ($i =0; $i < $size; $i + +) ... for ($i = 0; $i < $size; $i + +) ... $i = ($j < $size) 0:1; $i = ($j < $size)? 0:1;?
Operator Precedence
Do you know the detailed precedence of all the operators in PHP? I do not know. Don't guess. Always enforcing the precedence of an expression with parentheses can make the priority obvious, so that you know what it will do. For example:
/* What is the result? Who knows? * /$bool = ($i < 7 && $j > 8 | | $k = = 4); /* Now you're sure what I'm doing here. */ $bool = (($i < 7) && (($j < 8) | | ($k = = 4)))?
Database Naming conventions
- The name of the database and the data table is the same as the name of the class, named after the first uppercase camel. It is important to note that the names of all data tables must be in the plural notation.
- The names of all fields should be made up of descriptive words, and the named rules and variable naming conventions are consistent.
- To match the ORM model, we require all primary keys to be named uniformly as IDs
Other specifications
If and else if
When there are multiple conditions in a conditional statement, and there is a judgment of the value of the variable, it is necessary to put the judgment statement of the variable before other conditional statements.
/* Correct wording */if (1 = = = $val && function_exists (' Ob_gzhandler ')) { ...} /* Wrong notation */if (function_exists (' Ob_gzhandler ') && 1 = = = $val) {}
?
While the else if and ElseIf functions in PHP are essentially the same. But for the sake of the unity of the code (there is also the rumor that else if there is an unstable situation), we require that spaces be elseif between the lines:
if (2 = = $n) { ...} elseif (1 = = $n) { ...}
In the above code, I used 1 === $n this kind of writing, and did not use the common $n === 1 wording, so that the purpose is to avoid us to write a double equals sign incorrectly written an equal sign, and PHP will not error, which will lead to waste more time in the debugging process.
Here we do not force this type of writing, but it is strongly recommended that you put constants on the left side of the equals sign when you write equal logical judgments .
How to shorthand if statements
For the code to look a little shorter, the following simple if statement allows a shorthand approach:
The usual notation if ($a = = $b) { $c = ' foo ';} Abbreviated way $a = = = $b && $c = ' foo ';