Code specification for understanding the source files of PHP coding specifications

Source: Internet
Author: User
Start Comment

All source files should have a C-language-style annotation at the beginning, listing class name, function, version information, date, author, and copyright notices:


/*
* Class Name
* function
* Version
* Date
* Author
* Copyright
*/

If the file is modified, you should specify the purpose of the modification, the date of modification, the modification of the person, and change the version information of the file; If you modify a part of the file, you annotate it in the file and identify where you want to start and end the change.

......

/*
* Purpose of modification
* Date Modified
* Modified Person
* Version
*/

......

Modify Start

......

......

End of modification

......

3.2 Introducing Statements

The introduction statement should be located in the head of the file and, when introduced, explains the effect of introducing the file. For example:

Database Operations Class
Require ("db.php");

3.3 Declaration of Class

1 class document annotation (/**......*/) The information you want to include in this note, see document comments
2 Declaration of Class
3 class implementation annotations (/*......*/) if necessary, the annotation should contain any information about the entire class, which is not appropriate as a class documentation comment.
The 4 class (static) variable is first a public variable of the class, followed by a protection variable, then a variable at the package level (no access modifier, access modifier), and finally a private variable.
5 instance variables are public-level, followed by the protection level, then the package level (no access modifiers), and finally the private level.
6 constructors
7 Methods These methods should be grouped by function, not scope or access rights. For example, a private class method can be placed between two public instance methods. The goal is to make it easier to read and understand code

3.4 Indent Typesetting

4 spaces are often used as a unit of indent typesetting. The exact interpretation of indentation is not specified in detail (Space vs. tab). A tab is equal to 8 spaces (instead of 4), so in some editors, you need to specifically specify that the tab length is 4 (UltraEdit), and in some editors, the tabs are converted to spaces

3.5 Line Length

Try to avoid a line that is longer than 80 characters long because many terminals and tools are not well handled.

3.6 Line Change

When an expression cannot fit within a row, it can be broken by the following general rules:

-Disconnect after a comma
-Disconnect in front of an operator
-Prefer a higher-level (higher-level) fracture rather than a lower-level (lower-level) Disconnect
-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 up or stack your code on the right side, indent 8 spaces instead.

Here are some examples of disconnecting method calls:


SomeMethod (LongExpression1, LongExpression2, LongExpression3,
LongExpression4, LONGEXPRESSION5);

$var = SomeMethod1 (LongExpression1,
SOMEMETHOD2 (LongExpression2,
LongExpression3));

Here are two examples of broken arithmetic expressions. The former is better because the disconnect is outside the bracket expression, which is a higher level disconnect.


$longName 1 = $longName 2 * ($longName 3 + $longName 4-$longName 5)
+ 4 * $longname 6; Use this indentation method

$longName 1 = $longName 2 * ($longName 3 + $longName 4
-$longName 5) + 4 * $longname 6; Avoid this

The following is an example of two indentation method declarations. The former is a conventional case. The latter, if used in normal indentation, will move the second and third rows to the right, so instead indent 8 spaces


The traditional indentation method
function SomeMethod ($ANARG, $anotherArg, $YETANOTHERARG,
$andStillAnother) {
...
}

Use 8 consecutive spaces to avoid indentation of transitions
function Horkinglongmethodname ($ANARG,
$anotherArg, $yetAnotherArg,
$andStillAnother) {
...
}

The wrapping of an if statement usually uses a 8-space rule, because a regular indent (4 spaces) makes the statement body look more laborious. Like what:


Do not use this indentation method
if ((Condition1 && condition2)
|| (Condition3 && Condition4)
||! (Condition5 && Condition6)) {//Wrong line wrapping, no indentation
Dosomethingaboutit (); The condition is aligned with this sentence, causing the reading program to miss this sentence.
}

You should use this indentation method
if ((Condition1 && condition2)
|| (Condition3 && Condition4)
||! (Condition5 && Condition6)) {
Dosomethingaboutit ();
}

Or this indentation way can also
if ((condition1 && condition2) | | (Condition3 && Condition4)
||! (Condition5 && Condition6)) {
Dosomethingaboutit ();
}

Here are three possible ways to deal with ternary expressions:

$alpha = (alongbooleanexpression)? Beta:gamma;

$alpha = (alongbooleanexpression)? Beta
: Gamma;

$alpha = (alongbooleanexpression)
? Beta
: Gamma;



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.