A few things about the naming rules of PHP are described in detail

Source: Internet
Author: User
In writing code, a good coding standard, for us can play a lot of the effect of the intention. Like what:

1, improve the efficiency of our coding. Uniform code to facilitate our copy and paste Oh!

2, improve the readability of the code.

3, show our professional. Others see our code, found that the entire code of the writing process is uniform, instant force lattice on the up!

4, convenient team work together. Everyone uses the same specification, so that eliminates five flower eight minutes of writing way, the same coordination!

Develop a good habit of writing code

So what's the norm together?

File and Line

    • PHP code files must start with <?php;

    • The code must be indented using 4 space characters instead of the TAB key;

    • PHP code files must be UTF-8 encoded without a BOM;

    • Pure PHP code files must omit the last?> end tag;

    • All PHP files must end with a blank line;

    • All PHP files must use the Unix LF (linefeed) as the Terminator of the line;

    • The PHP code in the view file (except for the template code) must use the <?php?> long label or <?=?> short output label; no other custom labels are allowed.

    • After the non-empty line must not have extra space;

    • The number of characters per line should remain within 80 and should not be more than 120;

    • There must be more than one statement per line.

Class, method, property

    • Namespaces and classes must conform to the automatic loading specification for PSR: PSR-4;

    • In PHP code, you should only define classes, functions, constants, and so on, or only other operations that produce subordinate effects (such as generating file output, introducing files, modifying INI configuration files, and so on), which can only be selected.

    • The name of the class must follow the hump naming specification at the beginning of studlycaps capitalization;

    • Constants in a class all letters must be capitalized, and the words are separated by underscores;

    • The method name must conform to the CamelCase style of the lowercase start hump naming specification;

    • The use declaration must be placed after the namespace declaration, and after each namespace namespace declaration statement block and the use declaration statement block, a blank line must be inserted;

    • The opening curly braces ({) of a class and method must be written in a line after the function declaration, and the closing curly brace (}) must also be written in a line after the function body;

    • Keywords extends and implements must be written on the same line in the class name;

    • Each statement must not define more than one property, it must not use the keyword VAR to declare a property;

    • The properties and methods of the class must add access modifiers (private, protected, and public), and do not use underscores _ as prefixes to differentiate whether the attribute is protected or private;

    • When an abstract or final declaration needs to be added, it must be written before the access modifier, and the static must be written thereafter;

    • method and function declaration and invocation, there must be no space between the method name or the function name and the left parenthesis of the argument, there must be no space between the left parenthesis of the argument and the closing parenthesis, and the argument list has to have a space after each comma, and the comma must be preceded by a space.

PHP Naming conventions

Control structure

    • Control structure transitions (else, else if, catch, and do ... while in the structure) the closing curly braces (}) for the previous struct must be written in the same line, with a space between them.

    • The opening curly brace ({) of the control structure must be written on the same line as the declaration, and the ending Curly brace (}) must be written in a row after the body;

    • Control the structure of the keyword must have a space, a closing parenthesis) and the opening curly braces {There must also be a space;

    • The conditional parenthesis must not have a space before the closing parenthesis, and there must be a space between the two clause sentences (after the semicolon).

    • The body of each struct must be enclosed in pairs of curly braces.

Closed Package

    • The opening curly braces must be written in the same line of the Declaration, and the closing brace must follow the next line of the subject end;

    • When the closure declaration, keyword function and keyword use must have a space before and after;

    • There must be no spaces after the left parenthesis of the argument list and the variable list, and before the closing parenthesis;

    • In the arguments and variables list, there must be no spaces before commas, and spaces must be followed by commas.

Variables and keywords

    • All keywords in PHP must be all lowercase, constants true, FALSE, and null must all be lowercase;

    • Variable assignments and comparisons (including control structure condition statements and arrays) must have one space on each end of the operator (=, >, <, and = =), but the self-increment (+ +) self-decrement (-) Action variable must not have spaces between the symbols;

    • The variable declaration should use an underscore-delimited lowercase letter, should not use a non-generic word shorthand, should not appear outside the English spelling or shorthand;

    • Array, emtpy, Isset, unset and other key words in parentheses must not have extra spaces at both ends;

    • Array declaration and use should use [] instead of array ();

    • The declaration of the array variable needs to be wrapped, the array start symbol ([or array () should be on the same line as the operator, the first element of the array should start at a new line, and must be indented with the previous line, and the ending symbol ([or]) of the array must be a new row and should be aligned with the first character

Do not forget to beginner's mind, the party must always

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.