As the saying goes, no rules inadequate surrounding area. Program development is also the case, standardize the code neat, so that the program apes see is relaxed and happy. Even if the code only looks at itself, the code of the specification, self-feeling should be good. Moreover, in team development, the code can not only see themselves. Code of specification is a programmer's professional accomplishment, is to judge a program ape whether professional an important criterion. Now, follow the footsteps of the piglet and walk through the code specification of PHP.
I. Structure of the document
|
|――images
|――include
|――parameter
|――config
|――function
|――index
Images storage image files, include is the system is to refer to the file, generally in the parameter stored in the parameter file, config in the configuration file, function stored method files, such as JavaScript method, and according to the classification of functional modules, The classes for each feature are also put into them. (Well, it's important that the purpose of the opening and the kitchen to be and the bathroom in a different house is a reason, not explained.) )
Second, file name
Folder naming is generally used in English, the length is generally not more than 20 characters, named in lowercase letters. In addition to special cases to use Chinese pinyin, some common folder naming such as: Images (storage graphics file), Flash (store flash files), style (store css file), scripts (store JavaScript script), Inc. ( Store include files), link (store links), media (storing multimedia files), etc. File names are combined in lowercase letters, numbers, and underscores. (Here is the name of the folder, and the name of the clear specification.) such as xgmm.php, by, what is this, "blind to do Meimei"?? The original is to call "Change Password" Ah, see such a name, the programmer fell from the tree ... )
Specification of source file encoding
3.1 Opening comments
All source files should have a C-style comment at the beginning that lists the class name, feature, version information, date, author, and copyright notice:
1 /* 2 * Class name 3 * Function 4 * Version 5 * Date 6 * Author 7 * Copyright 8 */
If the file is modified, it should be in the file header to explain the purpose of modification, modify the date, modify the person, and change the version information of the file, if you modify the part of the file, you can comment in the file, and identify the beginning and end of the modified section
... /* * Modification Purpose * Revision date * Revision of person * version */ ...
// Modify Start ... .... // End of modification ...
(Note is very important, no annotated code, like no hint of classical Chinese, look very uncomfortable AH)
3.2 Introducing Statements
The introduction statement should be located at the head of the file and, when introduced, explain the effect of introducing the file. For example:
1 // 23require("db.php");
3.3 Class declarations write the order inside the class
1 class Document Comments (/**......*/) The information that you need to include in this comment, see the "documentation comment"
Declaration of Class 2
3 class implementation Note (/*......*/) if necessary, the comment should contain any information about the entire class, which is not appropriate as a class document comment.
The Class 4 (static) variable is first the public variable of the class, followed by the protection variable, then the package-level variable (no access modifier, access modifier), and finally the private variable.
The 5 instance variable is first public, followed by the protection level, then the package level (no access modifier), and finally the private level.
6 constructors
7 Methods These methods should be grouped by functionality, not scope or access rights. For example, a private class method can be placed between two public instance methods. The purpose is to make it easier to read and understand the code.
3.4 Indent Layout
4 spaces are often used as a unit of indentation layout. The exact explanation of indentation is not specified in detail (Space vs. tab). A tab is equal to 8 spaces (not 4), so in some editors you need to specifically specify that the tab length is 4 (UltraEdit), and in some editors, tabs are converted to spaces.
3.5 Line Length
Try to avoid a line that is longer than 80 characters because many terminals and tools cannot handle it well. (This is a historical reason, the previous unix,linux line length of more than 80 is not read, so, followed down.) It's too long to look good. )
3.6 Line break
When an expression cannot fit within a line, it can be broken according to the following general rules:
-Break after a comma
-Break in front of an operator
-rather than a lower-level (Lower-level) disconnection, choose a higher-level (higher-level) break
-The new line should be aligned with the beginning of the same level expression on the previous line
-If the above rules cause your code to clutter or make your code heap on the right, then indent 8 spaces instead.
Here are some examples of disconnection method calls:
1 2 longExpression4, longExpression5); 3 4 $var 5 6 LongExpression3));
The following is an example of a two break arithmetic expression. The former is better because the break is at the outside of the bracket expression, which is a higher level of disconnection.
1 $longName 1=$longName 2* ($longName 3+$longName 4-$longName 5)2+ 4 *$longname 6;//Use this indent method3 4 $longName 1=$longName 2* ($longName 3+$longName 4 5-$longName 5) + 4 *$longname 6;//avoid this
The following is an example of two indentation method declarations. The former is a general case. The latter, if used in regular indentation, will move the second and third rows to the right, so instead indent 8 spaces
// the traditional way of indentation function somemethod ($anArg$anotherArg$yetAnotherArg, $andStillAnother ) {... }// use 8 consecutive spaces to avoid a transition of indentation function horkinglongmethodname ($anArg, $anotherArg $yetAnotherArg, $andStillAnother) {... }
A line break for an if statement typically uses a 8-space rule, because a regular indent (4 spaces) makes the statement body look more laborious. Like what:
1 //do not use this indent method2 if((Condition1 &&condition2)3|| (Condition3 &&condition4)4||! (Condition5 && Condition6)) {//bad line wrapping, no indentation5Dosomethingaboutit ();//The condition is aligned with this sentence, causing the reading procedure to be missed.6 }7 8 //This indentation should be used in this way9 if((Condition1 &&condition2)Ten|| (Condition3 &&condition4) One||! (Condition5 &&condition6)) { A dosomethingaboutit (); - } - the //or it can be indented in such a way that - if((condition1 && condition2) | | (Condition3 &&condition4) -||! (Condition5 &&condition6)) { - dosomethingaboutit (); +}
There are three possible ways to work with ternary expressions:
1 $alpha = (alongbooleanexpression)? Beta: Gamma; 2 3 $alpha = (alongbooleanexpression)? Beta 4 : Gamma; 5 6 $alpha = (alongbooleanexpression)7 ? Beta 8 : Gamma;
Iv. Notes 4.1 block notes
Block annotations are typically used to provide descriptions of files, methods, data structures, and algorithms. Block annotations are placed at the beginning of each file and before each method. They can also be used elsewhere, such as within a method. Block annotations inside features and methods should have the same indentation format as the code they describe.
The top of the block comment should have a blank line that separates the block annotations from the code, such as:
1 /* 2 3 45* /
Block annotations can start with/*-so that indent (1) can recognize it as the beginning of a block of code, without having to rearrange it.
1 /* - 2 * If you want to be ignored, use special-format block annotations 3 4 *one 5 *6 * Three7*/
Note: If you do not use indent (1), you do not have to use/*-in your code or make concessions for others that might run indent (1) on your code.
(One-line comment will not say, this is not good to write, change the bar)
4.2 Documentation Comments
Document comments describe PHP classes, constructors, methods, and fields (field). Each document comment is placed in the comment delimiter/**...*/, and a comment corresponds to a class or member. The comment should precede the declaration:
1 /* * 2 * Description of some of this class ... 3 */ 4 class Example {...
Note that the top-level (top-level) class is not indented, and its members are indented. The first line (/**) of the document comment describing the class does not need to be indented, and subsequent document comments indent each line by 1 cells (the asterisk is aligned vertically). Members, including constructors, indent the first line of their document comments by 4, and each line is then indented by 5 cells.
If you want to give information about a class, variable, or method that is not suitable for writing in a document, you can use implementation block annotations (see 5.1.1) or a single-line comment immediately following the declaration (see 5.1.2). For example, the details of a class implementation should be placed in the implementation block comments immediately following the class declaration, rather than in the document comments.
Document comments cannot be placed in a definition block of a method or constructor, because the program will associate the first declaration that is located after the document comment.
V. Declaration 5.1 The number of variables declared per line
A single declaration is recommended because it facilitates writing comments. i.e.
1 $level // Degree of indentation 2 $size // determined by a tab
To be superior,
1 $level $size
Do not put declarations of different types of variables on the same line, for example:
1 $foo $fooarray // Error
Note: In the example above, a space is placed between the type and the identifier, and the other alternative is to use tabs:
1 $level // Degree of indentation 2 $size // determined by a tab 3 $currentEntry // tabs are usually selected as the standard for indentation
5.2 Initialization
Try to initialize while declaring the local variable. The only reason for not doing this is that the initial value of the variable depends on some previously occurring calculation. For example:
1 function functionname () {2 3 $var = 0; // initialization of local variables 4 5 $var 2 = getInt (); The local variable relies on the method getint, so it does not need to be initialized. 6 78 }
adjourned
PHP Code specification