PHP programming style specification sharing

  • 2020-12-10 00:40:01
  • OfStack

Note: EasyChen refers to C++ Development Specification of SINA Network Application Development Department, PHP4 Development Specification of Interactive Technology Department, and development specification of phpDocument. I think it is very good, suitable for the development of PHP, for your reference, it is necessary to develop a good programming style.

Chapter 1 Naming conventions

1.1 variable

1.1.1 Global variables

Global variables start with $g_, such as $g_data_list.

1.1.2 1 general variables

Variables like 1 are named with lowercase letters, and words are separated by an underscore.

Variable names should use a noun or adjective + noun. $value, $new_value.

1.1.3 Temporary variables

Do not use temporary variables such as $i, $j, etc. that are frequently used in the loop for other purposes.

1.2 the function

Functions are named with lowercase letters and words are separated by an underscore.

Naming functions suggests using a verb + noun, such as get_user_img.

The function that completes 1 set of functions is put in 1 file, the file that stores the function uses function_name.func.php name.

1.3 class

Class uses English case to separate words, including the first word, and all words are capitalized, such as PageManager;

In a class, methods are placed in front of attribute definitions, and public methods are placed in front of private methods.

1 In general, 1 class corresponds to 1 file;

When 1 class is closely related, it can be stored in 1 file.

The file containing the class is named ClassName.class.php.

1.4 methods

Use upper and lower case to separate the words. Capitalize the first letter of the other words, getCurrentPage();

Do not use unusual abbreviations, such as where2go();

Use only uppercase initials for common abbreviations, such as getHtml().

Chapter 2 Layout rules

2.1 Semantic separation

Each function, method should be used between blank line interval;

Statements that are closely related to a function may not be wrapped; in other cases, line wrapping is required.

2.2 Whitespace rule

2.2.1 Logical operators must be preceded and preceded by Spaces

correct

$a == $b; 

error


$a==$b;
$a ==$b;

correct

$a++; $a--; 

error

$a ++; $a --; 

Note the + 1-1 operator cannot be whitespace.

2.2.2 Spaces must be added when separating multiple parameters
correct


$g_pro , $g_user , g_show;  
get_db_info($host, $user, $passwd); 

error


$g_pro,$g_user,$g_show;  
get_db_info($host,$user,$passwd); 

2.2.3 Syntax keywords must be followed by Spaces

For example: If, for, while, switch... .
correct

for ($i = 0; $i < 10; $i++) 

error

for($i = 0; $i < 10; $i++ ) 

2.3 String and variable concatenation rules
String concatenation with variables using a '.' must be preceded by a space, and a '{}' must be preceded by a '.'
correct


$my_name = 'file_' . $var1;  
$my_name = "file_{$var1}"; 

error


$my_name = "file_'.$var1;  
$my_name = "file_$var1"; 

2.4 Rules of parenthesis
No Spaces are required after function names, and no Spaces are required after syntax keywords.
correct

for ($i = 0; $i < 10; $i++)  
strlen($my_name); 

error

for($i = 0; $i < 10; $i++ )  
strlen ($my_name); 

2.5 Rule of curly braces
Curly braces must correspond to upper and lower.

correct


if ($a)  
 {  
    $b = $a;  
 } 

error


if ($a){  
     $b = $a;  
 } 

2.6 Array definition rules

Single quotes must be used before and after the key value in array definition and usage.
PHP code:
correct


array( 'name'   => '', 'gender'  => '' );   
$user_info['name']; 

error


array( name => '', gender    => '' );   
$user_info[name];

2.7 SQL rules

The key words of PHP embedded in PHP are all in uppercase.
Table names and field names are enclosed in back quotes (') to prevent errors because field names contain Spaces;
Data values are enclosed in single quotes, and you should ensure that single quotes in data values have been escaped to prevent SQL injection.

correct

$sql = "SELECT `user`.`name` FROM `user` WHERE `id` = '$id' LIMIT 1"; 

error

$sql = "select name.user from name where id = $id "; 

Chapter 3 Comments the rules

3.1 General rules of 1
Don't write unnecessary comments; Add comments only if the code does not explain the logic well.
Think of annotations as part 1 of your program, writing/maintaining annotations as you write/maintain code;
The annotations follow the specification of PHPDocumentor to facilitate the generation of API level documents.

3.2 Detailed Rules
See the PHPDocumentor manual. Examples of the comments for each section are given below.

3.2.1 Copyright information
Note name copyright information
Explanatory notes:


$a==$b;
$a ==$b;
8

Note: Use // to mark the copyright information to avoid conflict with page-ES236en DocBlock

3.2.2 Examples of header comments

Comment name File header comment
Explanatory notes:

PHP code:


$a==$b;
$a ==$b;
9

note
1) The header annotation needs to indicate the package and subpackage;
2) Add $ID to @version for easy file management using CVS.

3.2.3 Examples of class annotations
Comment name class comment
Explanatory notes:

PHP code:

$a++; $a--; 
0

3.2.4 Example of class attribute annotation
Comment name class attribute comment
Explanatory notes:
PHP code:


/**
* Element type
*
* Type is used by many functions to skip the hassle of
*
* <code>
* if get_class($blah) == 'parserBlah'
* </code>
* always "inlinetag"
* @var string
*/
var $type = 'inlinetag';

3.2.5 Function/class method annotation examples
Comment name Function/class method comment
Explanatory notes:
PHP code:


/**
* @return string always ''
* calculate the short description of a DocBlock
* @see parserStringWithInlineTags::getString()
* @see parserStringWithInlineTags::trimmedStrlen()
*/
function getString()
{
 return '';
}


Related articles: