PHP Coding Specification
First, file format
1. For files that contain only PHP code, we will ignore "?>" at the end of the file. This is to prevent extraneous spaces or other characters from affecting the code.
For example:
$foo = ' Foo ';
2. Indentation should be able to reflect the logical results of the code, use four spaces as much as possible, and Disable Tab tab, as this guarantees flexibility across the client programmer software.
For example:
if (1 = = $x) {
$indented _code = 1;
if (1 = = $new _line) {
$more _indented_code = 1;
}
}
3. Variable assignments must be equally spaced and arranged.
For example:
$variable = ' demo ';
$var = ' Demo2 ';
4. Each line of code should be within 80 characters and up to 120 characters in length. Because the Linux read-in file is generally listed in 80 units, that is, if a line of code more than 80 characters, then the system will pay extra instructions for this. While this may seem like a minor problem, it is a standard that deserves attention and adherence to the perfect programmer.
5. No extra spaces are allowed at the end of each line.
Second, the naming convention
1. class files are suffixed with ". class.php", and the class file name allows only letters, which are named with the Hump method, and capitalized in the first letter, for example: DbMysql.class.php.
2. Files other than other class library files, such as configuration and functions, are usually suffixed with ". inc.php" and ". php", and the file name is named using lowercase letters and underscores, with multiple words separated by an underscore, such as config.inc.php, common.php,install_function.php.
3. Ensure that file naming and invocation are case-sensitive, because the case sensitivity is on top of Unix-like systems.
4. The class name and file name are the same (including the above capitalization), and the class name only allows letters, such as the Useraction class file name is UserAction.class.php, Infomodel class filename is InfoModel.class.php.
5. The Controller class is suffixed with an action, such as Useraction, infoaction, model classes are suffixed with models, such as Usermodel, Infomodel, and other classes are also suffixed with corresponding classifications, such as service, Widget.
6. The method name is only allowed by the letter, underline is not allowed, the first letter to lowercase, followed by each word in uppercase, that is, the so-called "hump law named" rule, and the more detailed the better, should be able to describe the function of the method, such as Switchmodel, Findpage.
7. The name of the attribute is only allowed by the letter, the underscore is not allowed, the first letter to lowercase, followed by each word in uppercase, that is, the so-called "hump law named" rules, such as Tableprefix, TableName.
8. For access to object members, we must always use the "get" and "set" methods. For example:
Class Foo
{
protected $_testobj;
Public Function Gettestobj ()
{
return $this->_testobj;
}
Public Function Settestobj ($TESTOBJ) {
$this->testobj = $_testobj;
}
}
9. When a class member method is declared as private, it must begin with a double underscore "__", respectively, and must be preceded by a single underscore "_" when declared as protected; The method is not underlined. For example:
Class Foo
{
Private Function __example () {
// ...
}
protected function _example () {
// ...
}
Public Function Example () {
// ...
}
}
10. If we need to define some of the frequently used methods as global functions, then we should define them in the class statically (static). For example:
Class Think
{
// ...
static public Function AutoLoad ($classname)
{
// ...
}
}
11. The class member property declared as private must begin with a double underscore "__", and the class member property declared as protected must begin with an underscore "_", whereas a member property declared as public is not allowed to have an underscore at any time.
12. The function is named with lowercase letters and underscores, and the more detailed the better, the function should be able to be described clearly, such as GET_CLIENT_IP.
13. Use "null" instead of "false" as the default value for a function parameter when the method or function parameter does not necessarily need to be assigned, unless the parameter is a Boolean value.
14. Variables are only allowed to be composed of lowercase letters and underscores, and the proposed naming of descriptive variables, the more detailed the better, so that like $i or $n, etc. are discouraged to use.
15. Constants in the class constant and globally in constant define, can only be composed of uppercase letters and underscores, each word is divided by an underscore.
Both the Boolean value and the null value are lowercase.
Third, coding style
1. The PHP code must be bound in full form ( ), i.e. do not use PHP short tags ( ), and ensure that you do not have any spaces after closing the label.
2. When a string is composed of plain text (that is, does not contain a variable), it must always be a single quotation mark (') as the delimiter. For example:
$a = ' Example String ';
3. Variables in variable substitution are only allowed in the form of $+ variable names. For example:
$greeting = "Hello $name, welcome back!"; Allow
$greeting = "Hello {$name}, welcome back!"; Allow
$greeting = "Hello ${name}, welcome back!"; Not allowed
When using the dot "." To concatenate strings, the string must be separated from the dot with a single space and allowed to be split into multiple lines to enhance readability. In this case, the dot "." must be aligned with the equals sign "=". For example:
$sql = "Select ' id ', ' name '". " From ' People ' "
. "WHERE ' name ' = ' Susan '"
. "ORDER by ' name ' ASC";
When you construct an array with an array type symbol, you must add a space after each comma to enhance readability. For example: $sampleArray = Array (1, 2, 3, ' Think ', ' SNS ');
4. When declaring an associative array using the array type, we encourage dividing it into multiple rows, except that we must simultaneously ensure that the keys of each row are aligned with the value to keep it beautiful. For example:
$sampleArray = Array (
' Firstkey ' = ' firstvalue ',
' Secondkey ' = ' secondvalue '
);
5. The opening of the curly brace must be shelf on the next line of the class name. For example:
Class Think
{
// ...
}
6. All code in the class must be indented with four spaces.
7. Each PHP file allows only one class to be declared. It is permissible to write other code in the class file, but it is not encouraged to do so. If you really want to attach the code, you must separate it with a blank line.
8. Declarations of any class variable must be placed at the top of the class, preceded by the declaration of any function.
9. The Var notation is not allowed to declare variables, and class member variables must be declared with private,protected and public. Second, it is generally better to use the get and set methods to access class members, although it is permissible to declare class members as public and refer directly to them.
10. The method must always declare its scope with private,protected or public.
11. Static Statics methods should declare their scope and should not be declared private privately, but should be protected or public, and if they do not want to inherit from the quilt class, they should be declared with final.
12. The initial curly brace of a function or method should be shelf on the next line of the function declaration. For example:
function Get_client_ip ()
{
...
}
13. No extra spaces are allowed between the function or method name and the parameter brackets. For example:
function Get_client_ip ()
{
...
}
14. References are only allowed to be defined in function arguments, and real-time pass-through references are forbidden. For example:
Reference definitions in function arguments-allowed
Function Definerefinmethod (& $a)
{
$a = ' a ';
}
Definerefinmethod ($b);
Echo $b; A
Real-time delivery of references-forbidden
function Calltimepassref ($a)
{
$a = ' a ';
}
Calltimepassref (& $c);
Echo $c; A
15. Function or method return value can not be wrapped in parentheses, otherwise it will reduce readability, and if later function is modified to return a reference, this will throw an exception.
16. Encourage the use of type hints as much as possible, especially in module designs. For example:
Class Foo
{
Public Function foo (someinterface $object)
{
}
Public Function bar (array $options) {
}
}
17. Function and method parameters must be separated by commas + spaces.
18. For a function whose parameters are arrays, the array in the parameter should be divided into multiple lines to enhance readability. For example:
Threearguments (Array (1, 2, 3), 2, 3);
Threearguments (Array (1, 2, 3, ' Think ',
' SNS ', $a, $b, $c,
56.44, $d, 500), 2, 3);
19. Based on the condition control of "if", "else" and "else if", we must open the statement and parentheses with a space interval, the opening of the curly brace "{" must be on the same line as the conditional control statement, the end "}" must always be exclusive one row and shelf, the control flow content must be indented with four spaces, And do not use "ElseIf".
if ($condition) {
// ...
} else if ($_condition) {
// ...
} else {
// ...
}
20. Within the conditional parentheses of the conditional control statement, the operator must be separated from the other elements with a space. If you encounter a long logical decision, you are encouraged to use the enclosing parentheses to separate the logic. For example:
if ($a! = 2) and ($b = = 1)) {
$a = $b;
}
In the "switch" conditional control statement, the parameter to be measured must be separated from other elements with a space. For example:
Switch ($num) {
...
}
The contents of the "switch" statement must be indented with four spaces, and the content controlled by the "case" condition must be indented with four additional spaces. For example:
Switch ($indentedSpaces) {
Case 2:
echo "Error";
Break
Case 4:
echo "Right";
Break
Default
Break
}
23. The "Default" control should always be included in the "switch" statement.
24. Sometimes we need to omit "break" or "return" in the "case" context, at which point we must add "//No break Here" comments for these "case" statements. For example:
Switch ($numPeople) {
Case 1://No break here Case 2:
Break
Default
Break
}
http://www.bkjia.com/PHPjc/992670.html www.bkjia.com true http://www.bkjia.com/PHPjc/992670.html techarticle PHP Coding specification One, file format 1. For files that contain only PHP code, we will ignore them at the end of the file. This is to prevent extra spaces or other characters from affecting the generation ...