PHP and php encoding specifications
I. File Format
1. For files that only contain php code, we will ignore them at the end of the file. "?> ". This is to prevent extra spaces or other characters from affecting the code.
For example:
<? Php
$ Foo = 'foo ';
2. indentation should be able to reflect the logic results of the Code. Try to use four spaces and do not use TAB tabs, because this ensures the flexibility of cross-client programming software.
For example:
If (1 ==$ x ){
$ Indented_code = 1;
If (1 = $ new_line ){
$ More_indented_code = 1;
}
}
3. Variable values must be given equal spacing and arrangement.
For example:
$ Variable = 'Demo ';
$ Var = 'demo2 ';
4. The length of each line of code should be limited to 80 characters, up to 120 characters. Because linux reads files in the unit of 80 columns, that is to say, if a line of code contains more than 80 characters, the system will pay additional operation instructions for this. Although this seems to be a small problem, it is also a noteworthy and compliant standard for programmers pursuing perfection.
5. No extra spaces are allowed at the end of each line.
Ii. Naming Conventions
1. All class files are suffixed with ". class. php", and the class file name can only contain letters. Use the hump method and use uppercase letters, for example, DbMysql. class. php.
2. files other than other class library files such as configuration and functions are generally named ". inc. php "and ". php "is a suffix, and the name of the file name is named by lowercase letters and underscores. Multiple words are separated by an underscore (_), for example, config. inc. php, common. php and install_function.php.
3. Ensure that the file name is the same as the call case, because it is case sensitive on Unix-like systems.
4. the class name and file name are the same (including the same case), and only letters are allowed for the class name. For example, the file name of the UserAction class is UserAction. class. the file name of the php and InfoModel classes is InfoModel. class. php.
5. the Controller class is suffixed with Action, such as UserAction and InfoAction. The Model class is suffixed with Model, such as UserModel and InfoModel. Other classes are also suffixed with corresponding categories, such as Service and Widget.
6. the method name can only consist of letters, underlines are not allowed, the first letter must be lowercase, and then the first letter of each word must be capitalized, that is, the so-called "camper naming" rule, the more detailed the method, the better. You should be able to clearly describe the functions of this method, such as switchModel and findPage.
7. attribute naming can only consist of letters, underlines are not allowed, the first letter must be lowercase, and then the first letter of each word must be capitalized, that is, the so-called "Hump naming" rule, for example, tablePrefix and 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 the class member method is declared as private, it must start with a double underline "_"; when it is declared as protected, it must start with a single underline; generally, the method does not contain underscores. For example:
Class Foo
{
Private function _ example (){
//...
}
Protected function _ example (){
//...
}
Public function example (){
//...
}
}
10. If we need to define some frequently used methods as global functions, we should define them in the class in static form. For example:
Class Think
{
//...
Static public function autoload ($ classname)
{
//...
}
}
11. the attribute of a class member declared as private must begin with a double underline "_"; the attribute of a class member declared as protected must begin with an underscore; the member attribute declared as public cannot contain underscores at any time.
12. The function name should be in the lower-case letter and underline mode, and the more detailed the better, you should be able to clearly describe the function, such as get_client_ip.
13. When a method or function parameter does not need to be assigned a value, use "null" instead of "false" as the default value of the function parameter unless the parameter is a boolean value.
14. variables can only consist of lower-case letters and underscores. It is recommended that you use descriptive variables. The more detailed the variable, the better, so that $ I or $ n is not encouraged.
15. The constant and define constants in the global range in the class can only consist of uppercase letters and underscores. Each word is separated by an underscore.
16. Both boolean and null values are in lower case.
Iii. Encoding Style
1. php code must be fully bounded (<? Php... ?> ), That is, do not use php short labels (<? ... ?> ), Make sure that no space is allowed after the label is disabled.
2. When a string is composed of plain text (that is, it does not contain variables), it must always use single quotation marks (') as the delimiter. For example:
$ A = 'example string ';
3. variables in the replace of variables can only be 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 you use periods "." To connect strings, separate them with a space and separate them into multiple lines to improve readability. In this case, the period "." must be aligned with the equal sign "=. For example:
$ SQL = "SELECT 'id', 'name'". "FROM 'people '"
. "WHERE 'name' = 'Susan '"
. "Order by 'name' ASC ";
When an array is constructed with an array symbol, a space must be added after each comma to enhance readability. Example: $ sampleArray = array (1, 2, 3, 'think', 'sns ');
4. When the array type operator is used to declare the associated array, we encourage it to be divided into multiple rows, but we must ensure the alignment of the keys and values of each row at the same time to keep it beautiful. For example:
$ SampleArray = array (
'Firstkey' => 'firstvalue ',
'Secondkey' => 'secondvalue'
);
5. the braces must start with the top of the next line of the class name. For example:
Class Think
{
//...
}
6. All code in the class must be indented with four spaces.
7. Only one class can be declared in each php file. Writing other code in a class file is allowed, but this is not encouraged. If code is to be appended, it must be separated by blank lines.
8. The declaration of any class variable must be placed on the top of the class, prior to the declaration of any function.
9. Variables cannot be declared using the var symbol. class member variables must be declared using private, protected, and public. Second, although it is allowed to declare the class members as public and directly reference them, it is usually better to use the get and set methods to declare class members.
10. The method must always use private, protected, or public to declare its scope.
11. The static method should declare its scope and should not be declared as private, but should be protected or public. If you just don't want to inherit from the quilt class, you should use final to declare them.
12. The initial braces of the function or method should be in the top of the next line of the function declaration. For example:
Function get_client_ip ()
{
//...
}
13. Unnecessary spaces are not allowed between function or method names and parameter brackets. For example:
Function get_client_ip ()
{
//...
}
14. The reference can only be defined in function parameters. It is forbidden to pass the reference in real time. For example:
// Reference is defined in the function parameter-Allowed
Function defineRefInMethod (& $)
{
$ A = 'a ';
}
DefineRefInMethod ($ B );
Echo $ B; // 'A'
// Transfer the reference in real time-prohibited
Function callTimePassRef ($)
{
$ A = 'a ';
}
CallTimePassRef (& $ c );
Echo $ c; // 'A'
15. The function or method return value cannot be enclosed in parentheses. Otherwise, the readability will be reduced. If the function is changed to return reference, an exception will be thrown.
16. Try to use the type prompts, especially in the module design. For example:
Class Foo
{
Public function foo (SomeInterface $ object)
{
}
Public function bar (array $ options ){
}
}
17. function and method parameters must be separated by commas (,) and spaces.
18. For functions whose parameters are arrays, the arrays in the parameters should be divided into multiple rows 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. in condition Control Based on "if", "else", and "else if", we must use spaces to separate the statement and brackets, the start of the braces "{" must be in the same line as the condition control statement, and the end "}" must always exclusive to one row and the top level. The control process content must be indented with four spaces, "elseif" is not used ".
If ($ condition ){
//...
} Else if ($ _ condition ){
//...
} Else {
//...
}
20. In the condition brackets of the condition control statement, the operator must be separated by spaces from other elements. If you encounter a long logical judgment, you are encouraged to use embedded brackets to separate each logic. For example:
If ($! = 2) and ($ B = 1 )){
$ A = $ B;
}
21. In the "switch" condition control statement, the parameters to be tested must be separated by spaces from other elements. For example:
Switch ($ num ){
//...
}
22. The content of the "switch" statement must be indented with four spaces, and the content under the "case" condition must be indented with four spaces. For example:
Switch ($ indentedSpaces ){
Case 2:
Echo "error ";
Break;
Case 4:
Echo "correct ";
Break;
Default:
Break;
}
23. "default" control should always be included in the "switch" statement.
24. sometimes we need to omit "break" or "return" in the context of "case". In this case, we must add "// here there is no break" comment for these "case" statements. For example:
Switch ($ numPeople ){
Case 1: // here there is no breakcase 2:
Break;
Default:
Break;
}