Home >Backend Development >PHP Tutorial >PHP Recommended Standard PSR PHP Program Coding Specification Standard PHP Standard Deviation PHP Network Programming Standard Teaching
PSR is the abbreviation of PHP Standards Recommendation.
PSR-1: Basic coding style
PHP tags
PHP code must be placed in <?php ?>
or <?= ?>
tags. No other PHP tag syntax may be used.
Encoding
All PHP files must be encoded using the UTF-8 character set and cannot have a byte order mark
Purpose
A PHP file can define symbols (classes, properties, functions, constants, etc.), or perform operations with side effects (such as generating results or processing data), but not both at the same time.
Auto loading
PHP namespaces and classes must comply with the PSR-4 autoloader standard.
Name of class
PHP class names must always use CamelCase.
The name of the constant
PHP constant names must use all uppercase letters. If necessary, use underscores to separate words.
The name of the method
PHP method names must always use camelCase. That is, the first letter of the method name is lowercase, and the first letters of subsequent words are uppercase.
PSR-2: Strict coding style
implement PSR-1
Implement PSR-1 coding style before using PSR-2 coding style.
Indentation
The PSR-2 recommendation requires that PHP code be indented with four spaces.
Files and lines of code
PHP files must use style line breaks (LF), have a blank line at the end, and cannot use the PHP closing tag ?>. Each line of code cannot exceed 80 characters, or at least 120 characters. There cannot be spaces at the end of each line.
Keywords
The PSR-2 recommended specification requires that all keywords should be in lowercase letters.
Namespace
Each namespace declaration statement must be followed by a blank line. Similarly, when using the use keyword to import a namespace or create an alias for a namespace, add a blank line after a series of use declarations.
Category
The PSR-2 recommended specification requires that the opening brackets of the class definition body should be written on a new line after the class name. The closing bracket of the class definition body must be written on a new line after the definition body. If a class extends other classes or implements interfaces, the extends or implements keyword must be written on the same line as the class name.
Method
The bracket position of the method definition body is the same as that of the class definition body: the opening bracket of the method definition body must be written on a new line after the method name; the closing bracket of the method definition body must be written on a new line after the method definition body. Pay special attention to the method parameters: there are no spaces after the opening parenthesis and no spaces before the closing parenthesis. Each method parameter (except the last one) is followed by a comma and a space.
Visibility
Every property and method in a class must declare visibility. If you declare class properties and methods as abstract or final, these two qualifiers must be placed before the visibility keyword. If properties and methods are declared static, this 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 after the control structure keyword, there must be no spaces after the opening parenthesis and no spaces before the closing parenthesis. Unlike the definition body of classes and methods, the opening bracket after the control structure keyword should be written on the same line as the control structure keyword. The closing parenthesis after a control structure keyword must be written on a separate line.
PSR-3: Logger interface
PSR-4: Autoloader
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });The above has introduced the PHP recommended standard PSR, including PHP and standard content. I hope it will be helpful to friends who are interested in PHP tutorials.