Home >php教程 >php手册 >PHP programming style specification sharing_php basics

PHP programming style specification sharing_php basics

WBOY
WBOYOriginal
2016-05-16 08:59:542982browse

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

Copy code The code is as follows:
$a == $b;

Error

Copy code The code is as follows:

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

Correct

Copy code The code is as follows:
$a ; $a--;

Error

Copy code The code is as follows:
$a ; $a --;
P>

Remarks: The addition and subtraction operators cannot add spaces.

2.2.2 Spaces must be added when separating multiple parameters
Correct

Copy code The code is as follows:

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

Error

Copy code 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 syntax keywords

For example: If, for, while, switch…..
Correct

Copy code The code is as follows:
for ($i = 0; $i

Error

Copy code The code is as follows:
for($i = 0; $i

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

Copy code The code is as follows:

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

Error

Copy code The code is as follows:

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

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

Copy code The code is as follows:
for ($i = 0; $i strlen($my_name);

Error

Copy code The code is as follows:
for($i = 0; $i strlen ($my_name);

2.5 Curly Brace Rules
The curly braces must correspond to the upper and lower parts.

Correct

Copy code The code is as follows:

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

Error

Copy code The code is as follows:

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

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

Copy code The code is as follows:

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

Error

Copy code The code is as follows:

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

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

Copy code The code is as follows:
$sql = "SELECT `user`.`name` FROM ` user` WHERE `id` = '$id' LIMIT 1";

Error

Copy code The code is as follows:
$sql = "select name.user from name where id = $ id ";

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:

Copy code The code is as follows:

//
// ---- ----------------------------------------
// | phpDocumentor |
// --------------------------------------- -------------
// | Copyright (c) 2000-2003 Joshua Eichorn |
// | Email jeichorn@phpdoc.org |
// | Web http ://www.phpdoc.org |
// ---------------------------------------- ----------------
// | This source file is subject to PHP License |
// -------------- -------------------------------------
//

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:

Copy 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 $
*/
div>

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:

Copy 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 Class Attribute Annotation Example
Annotation Name Class Attribute Annotation
Annotation Demonstration:
PHP Code:

Copy code The code is as follows:

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

3.2.5 Example of function/class method annotation
Annotation name Function/class method annotation
Example of annotation:
PHP code:

Copy 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 '';
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn