EasyChen draws on the SINA Network Application Development department C ++ development specifications, the interaction technology department PHP4 development specifications, and the phpDocument development specifications. I think it is very good and suitable for PHP Development. for your reference, develop a good programming style: easyChen draws on the SINA Network Application Development department C ++ development specifications, the interaction technology department PHP4 development specifications, and the phpDocument development specifications. I think it is very good and suitable for PHP Development. for your reference, it is very necessary to develop a good programming style.
Chapter 2 naming rules
1.1 variables
1.1.1 Global variables
Global variables start with $ g _, for example, $ g_data_list.
1.1.2 General variables
Generally, variables are named by lowercase letters, and words are separated by underscores.
Variable names should use nouns, adjectives, and nouns. For example, $ value and $ new_value.
1.1.3 Temporary variables
Do not use temporary variables such as $ I and $ j that are frequently used in loops for other purposes.
1.2 Functions
The function is named by lowercase letters, and words are separated by underscores.
We recommend that you use the verb + noun method for function naming, such as get_user_img.
The functions that complete a set of functions are put into a file, and the files that store the functions are named by function_name.func.php.
1.3 Categories
Classes use English uppercase and lowercase letters to separate words, including the first word, and uppercase letters of all words, such as PageManager;
In the class, the method is placed in the attribute definition front edge, and the public method is placed in the special method front edge;
Generally, a class corresponds to a file;
When some classes are closely related, they can be stored in a file;
Class files are named in ClassName. class. php format.
1.4 method
Use English case to separate words. except the first word, the first letter of other words is capitalized, such as getCurrentPage ();
Do not use uncommon abbreviations, such as where2go ();
When common abbreviations are used, only the first letter is capitalized, such as getHtml ().
Chapter 3 layout rules
2.1 Semantic separation
Empty row intervals should be used between functions and methods;
Line breaks are not allowed 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
The code is as follows: $ a = $ B;
Error
The code is as follows:
$ A = $ B;
$ A = $ B;
Correct
The code is as follows: $ a ++; $ --;
Error
The code is as follows: $ a ++; $ --;
The note plus minus one operator cannot contain spaces.
2.2.2 separate multiple parameters with spaces
Correct
The code is as follows:
$ G_pro, $ g_user, g_show;
Get_db_info ($ host, $ user, $ passwd );
Error
The code is as follows:
$ G_pro, $ g_user, $ g_show;
Get_db_info ($ host, $ user, $ passwd );
2.2.3 spaces must be added after the syntax keyword
For example: If, for, while, switch .....
Correct
The code is as follows: for ($ I = 0; $ I <10; $ I ++)
Error
The code is as follows: for ($ I = 0; $ I <10; $ I ++)
2.3 string and variable connection rules
When the '.' sign is used for string-to-variable connection, spaces must be added before and after '.'. when the "sign is used," {} "must be added before and after the variable "{}".
Correct
The code is as follows:
$ My_name = 'File _ '. $ var1;
$ My_name = "file _ {$ var1 }";
Error
The code is as follows:
$ My_name = "file _ '. $ var1;
$ My_name = "file _ $ var1 ";
2.4 Parentheses
You do not need to add spaces to the brackets behind the function name or to the brackets after the syntax keyword.
Correct
The code is as follows: for ($ I = 0; $ I <10; $ I ++)
Strlen ($ my_name );
Error
The code is as follows: for ($ I = 0; $ I <10; $ I ++)
Strlen ($ my_name );
2.5 curly brackets
Curly braces must be upper and lower matched.
Correct
The code is as follows:
If ($)
{
$ B = $;
}
Error
The code is as follows:
If ($ ){
$ B = $;
}
2.6 array definition rules
Single quotation marks must be added before and after the key value in array definition and usage.
PHP code:
Correct
The code is as follows:
Array ('name' => '', 'gender' => '');
$ User_info ['name'];
Error
The code is as follows:
Array (name => '', gender => '');
$ User_info [name];
2.7 SQL rules
All SQL statement keywords embedded in PHP are in uppercase;
The table name and field name must be enclosed in reverse quotation marks (') to prevent errors because the field name contains spaces;
The data values are enclosed by single quotation marks (''), and the single quotation marks in the data values must be escaped to prevent SQL injection.
Correct
The code is as follows: $ SQL = "SELECT 'user'. 'name' FROM 'user' WHERE 'id' = '$ ID' LIMIT 1 ";
Error
The code is as follows: $ SQL = "select name. user from name where id = $ id ";
Chapter 4 comment rules
3.1 General rules
Unnecessary comments are not written; comments are used only when the code cannot describe the logic well;
Regard comments as part of a program and write/maintain comments when writing/maintaining code;
The annotation uses the PHPDocumentor specification to generate API-level documents.
3.2 detailed rules
See PHPDocumentor manual. The comments and examples of each part are given below.
3.2.1 copyright information
Annotation name copyright information
Annotation example:
The code is as follows:
//
// + ------------------------------------------------------ +
// | PhpDocumentor |
// + ------------------------------------------------------ +
// | Copyright (c) 2000-2003 Joshua Eichorn |
// | Email jeichorn@phpdoc.org |
// | Http://www.phpdoc.org |
// + ------------------------------------------------------ +
// | This source file is subject to PHP License |
// + ------------------------------------------------------ +
//
Note: Use // to indicate the copyright information to avoid conflict with the page-level DocBlock of PHPDocumentor.
3.2.2 File header annotation example
Annotation name File header annotation
Annotation example:
PHP code:
The code is as follows:
/**
* All abstract representations of inline tags are in this file
* @ Package phpDocumentor
* @ Subpackage InlineTags
* @ Since separate file since version 1.2
* @ Version $ Id $
*/
Remarks
1) the file header comment must specify the package and sub-package;
2) add $ ID to @ version to facilitate file management using CVS.
3.2.3 class annotation example
Annotation name class annotation
Annotation example:
PHP code:
The code is as follows:
/**
* Use this element to represent an {} inline tag} like {} link}
* @ See parserStringWithInlineTags
* @ Package phpDocumentor
* @ Subpackage InlineTags
* @ Author Greg Beaver
* @ Since 1.0rc1
* @ Version $ Revision: 1.21.2.6 $
* @ Tutorial inlinetags. pkg
*/
3.2.4 example of class property annotation
Annotation name class attribute annotation
Annotation example:
PHP code:
The code is as follows:
/**
* Element type
*
* Type is used by using functions to skip the hassystemic
*
*
* if get_class($blah) == 'parserBlah'
*
* Always "inlinetag"
* @ Var string
*/
Var $ type = 'inlinetag ';
3.2.5 function/class method annotation example
Annotation name function/class method annotation
Annotation example:
PHP code:
The code is as follows:
/**
* @ Return string always''
* Calculate the short description of a DocBlock
* @ See parserStringWithInlineTags: getString ()
* @ See parserStringWithInlineTags: trimmedStrlen ()
*/
Function getString ()
{
Return '';
}