PHP recommended standard PSR PHP code specification standard PHP standard deviation PHP network programming standard Teaching

Source: Internet
Author: User
PSR is the abbreviation for PHP standards recommendation.

PSR-1: The basic code style
PHP tags
You must put the PHP code in the or tag. Do not use other PHP tag syntax.
Coding
All PHP files must be encoded using the UTF-8 character set and cannot have byte order marks
Objective
A PHP file can define symbols (classes, traits, functions, constants, and so on), or perform actions that have side effects (such as generating results or processing data), but cannot do both things at the same time.
Auto Load
PHP namespaces and classes must adhere to the PSR-4 Auto loader standard.
The name of the class
The name of the PHP class must always use the camel (CamelCase).
The name of the constant
The names of PHP constants must all be in uppercase letters. If you want, you can use underscores to separate the words.
The name of the method
The name of the PHP method must always be used CamelCase this camel style. In other words, the first letter of the method name is lowercase, and the first letter of the subsequent word is capitalized.

PSR-2: A strict code style
Implement PSR-1
Implement the PSR-1 code style before using the PSR-2 code style.
Indent in
The PSR-2 recommendation specification requires PHP code to be indented with four spaces.
Files and lines of code
The PHP file must use a style newline (LF), a blank line at the end, and the tag?> cannot be closed using PHP. Each line of code cannot exceed 80 characters, or at least 120 characters. There must be no spaces at the end of each line.
Key words
PSR-2 recommendation specification requires that keywords should use lowercase letters.
Name space
Each namespace declaration statement must follow a blank line. Similarly, when you import a namespace by using the Use keyword or create an alias for a namespace, a blank line is added after a series of usage declaration statements.
Class
The PSR-2 recommendation specification requires that the starting parenthesis of a class definition body should be written in a new line after the class name. The end parenthesis of the class definition body must be written a new line after the body of the definition. If the class extends another class or implements an interface, the extends or implements keyword must be written on the same line as the class name.
Method
The parentheses position of the method definition body is the same as the bracket position of the class definition body: The starting parenthesis of the method definition body is a new line after the method name, and the end symbol of the method definition body is written in a new line after the method definition body. Pay special attention to the parameters of the method: there are no spaces after the starting parenthesis, and no spaces before closing the parentheses. Each parameter of the method (except the last one) is followed by a comma and a space.
Visibility of
Each property and method in the class must declare the visibility. If class properties and methods are declared abstract or final, both qualifiers must precede the visibility keyword. If attributes and methods are declared static, the qualifier must be placed after the visibility keyword.
Control structure
All control structure keywords must be followed by a space. If there is a pair of parentheses behind the control structure keyword, there must be no spaces after the starting parenthesis, and no spaces before closing the parentheses. Unlike the definition body of a class and method, the starting parenthesis after the control structure keyword should be written on the same line as the control structure key. The closing parenthesis after the control structure keyword must be written separately on one line.

PSR-3: Logger interface

PSR-4: Auto Loader

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP recommended standard PSR, including PHP, standard aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.