PHP coding specification _ PHP Tutorial

Source: Internet
Author: User
PHP coding specifications. PHP coding specification 1. File Format 1. for files that only contain php code, we will ignore it at the end of the file ?. This is to prevent extra spaces or other characters from affecting the code specification of the PHP code generation.

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:
$ 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 bounded in the complete form ( ), That is, do not use php short labels ( .
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 break case 2:
Break;
Default:
Break;
}

Http://www.bkjia.com/PHPjc/992670.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/992670.htmlTechArticlePHP Coding Specification 1. File Format 1. for files that only contain php code, we will ignore it at the end of the file? . This is to prevent extra spaces or other characters from affecting the generation...

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.