Note: This specification is compiled by EasyChen based on the "C Development Specification" of the SINA Network Application Development Department, the "PHP4 Development Specification" of the Interactive Technology Department, and the phpDocument specification. I think it is very good and suitable for PHP development. It is a reference for everyone. It is very necessary to develop a good programming style.
Chapter 1 Naming Convention
1.1 Variables
1.1.1 Global variables
Global variables start with $g_, such as $g_data_list.
1.1.2 General variables
Generally, variables are named with lowercase letters, and words are separated by underscores.
Variable names should be in the form of nouns or adjectives. Such as $value, $new_value.
1.1.3 Temporary variables
Do not use temporary variables such as $i, $j, etc. that are frequently used in loops for other purposes.
1.2 Function
Functions are named with lowercase letters, and words are separated by underscores.
It is recommended to use verb noun when naming functions, such as get_user_img.
The functions that complete a set of functions are placed in a file, and the file storing the functions is named function_name.func.php.
1.3 Class
The class uses English uppercase and lowercase to separate words, including the first word, and the first letter of all words is capitalized, such as PageManager;
In a class, put methods before attribute definitions and public methods before special methods;
Generally, a class corresponds to a file;
When some classes are closely related, they can be stored in one file;
The file that stores the class is named ClassName.class.php.
1.4 Method
The method uses English uppercase and lowercase to separate words. Except for the first word, the first letters of other words are capitalized, such as getCurrentPage();
Do not use uncommon abbreviations, such as where2go();
When using commonly used abbreviations, only capitalize the first letter, such as getHtml().
Chapter 2 Format Rules
2.1 Semantic Separation
Blank lines should be used between each function and method;
There is no need for line breaks between closely related statements in the same function. In other cases, line breaks are required.
2.2 Space rules
2.2.1 Spaces must be added before and after logical operators
Correct
Error
Correct
Error
Remarks: The addition and subtraction operators cannot add spaces.
2.2.2 Spaces must be added when separating multiple parameters
Correct
Error
2.2.3 Spaces must be added after syntax keywords
For example: If, for, while, switch…..
Correct
Error
2.3 String and variable connection rules
When strings and variables are connected using the '.' sign, spaces must be added before and after the '.'. When using the "." sign, "{}" must be added before and after the variable.Correct
Error
2.4 Parentheses Rules
There is no need to add spaces after the function name, and spaces must be added after the syntax keywords.
Correct
Error
2.5 Curly Brace Rules
The curly braces must correspond to the upper and lower parts.
Correct
Error
2.6 Array definition rules
When defining and using an array, single quotes must be added before and after the key value.
PHP code:
Correct
Error
2.7 SQL Rules
SQL statement keywords embedded in PHP should all be in uppercase;
Table names and field names should be enclosed in backticks (`) to prevent errors caused by spaces in the field names. An error occurred;
The data value should be surrounded by single quotes'', and you should ensure that the single quotes in the data value have been escaped to prevent SQL injection.
Correct
Error
Chapter 3 Comment Rules
3.1 General Rules
Do not write unnecessary comments; only when the code cannot explain the logic well, use comments to supplement it;
Think of comments as Part of the program, write/maintain comments while writing/maintaining code;
The comments completely adopt the specifications of PHPDocumentor to facilitate the generation of API-level documents.
3.2 Detailed rules
Please refer to the PHPDocumentor manual. Annotation examples for each part are given below.
3.2.1 Copyright information
Annotation name Copyright information
Example of annotation:
Note: Use // to mark copyright information to avoid conflict with PHPDocumentor's page-level DocBlock
3.2.2 File header comment example
Comment name file header comment
Comment Demonstration:
PHP code:
Remarks
1) The file header comment needs to indicate the package and sub-package it belongs to;
2) Add $ID to @version to facilitate file management using CVS.
3.2.3 Class annotation example
Annotation name Class annotation
Example of annotation:
PHP code:
<br>* if get_class($blah) == 'parserBlah'<br>*
3.2.5 Example of function/class method annotation
Annotation name Function/class method annotation
Example of annotation:
PHP code: