Home  >  Article  >  Backend Development  >  PHP coding style specification detailed introduction

PHP coding style specification detailed introduction

angryTom
angryTomforward
2019-08-09 16:44:323055browse

PHP coding style specification detailed introduction

Due to the flexibility of PHP, many people do not pay attention to a good code specification when writing code, making the already flexible PHP code look messy. In fact, the PSR specification PSR-1 and PSR-2 have defined some specifications in PHP coding. As long as we follow these specifications well, we can write very beautiful and neat code even when using a flexible scripting language. First, let’s take a look at the passed PSR specifications, and then briefly explain some of the specific requirements of the PSR-1 and PSR-2 specifications.

Passed PSR

##PSR numberNameDescription##123467

PSR-1 Basic Coding Standard

1. Open and close tags

First, the PHP code must start with Starts with a daabcad2f44d64fe9ad8e8983f94ac69 tag, but should end with a blank line.

2. A blank line should be inserted after the namespace declaration, and there should also be a blank line after the use statement block .

Don’t make multiple use statements in the same line of code.

3. The beginning and end of the class

The class keyword, class name, and the extends and implements keywords must be on the same line. If a class implements multiple interfaces, the interface names can be on the same line of the class declaration, or they can occupy separate lines. If you choose to place these interface names on multiple lines, the first interface name must be on its own line and not follow the implements keyword. The opening brace ({) of a class should be written on its own line after the function declaration, and the closing brace (}) should also be written on its own line after the class body. That is, the class declaration looks like the following

class EarthGame extends Game implements
    Playable,    
    Savable
{ 
       //类体
}

It is also possible to put the class name on the same line as the class declaration.

class EarthGame extends Game implements Playble, Savable
{ 
    //类体
 }

4. Attribute declaration

Each attribute must have an access modifier (public, private or protected). Attributes cannot be declared using the keyword var. The specification of attribute names is already covered in PSR-1: you can use underscores, lowercase camelCase naming, or uppercase camelCase naming, but should remain consistent. (Personally recommend using lowercase camel case for attributes)

5. The beginning and end of the method

All methods must have access modifiers (public, private or protected). The access modifier must be after abstract or final and before static. Method parameters with default values ​​should be placed at the end of the parameter list.

 ●Single-line declaration
  The opening curly brace ({) of a method should be written on its own line after the method name, and the closing curly brace (}) should also be written on its own line after the method body (directly following after the method code). Method parameter lists should not start or end with spaces (i.e. they should follow the parentheses surrounding them). For each parameter, there should be a comma after the parameter name (or default value), and a space after the comma. This may sound complicated, as shown below:

final public static function generateTile(int $diamondCount, bool $polluted = false)
{
   //方法体
}

Multi-line declaration
If the method has many parameters, then a single-line method declaration is not practical. At this point we can split the parameter list so that each parameter (including type, parameter variable, default value, and comma) is on its own indented line. In this case, the closing parenthesis should be placed on the line after the parameter list, aligned with the beginning of the method declaration. The opening curly brace ({) should follow the closing parenthesis on the same line, separated by a space. The method body should start on a new line. Again, this may sound complicated, but the following example should help you understand this rule.

public function __construct(
    int $size,
    string $name,
    bool $warparound = false,
    bool $aliens = false
) {
  //方法体
 }

6. Lines and indentation

Code should be indented using 4 spaces instead of tabs. We can check the editor settings and set it to use 4 spaces instead of tabs when the tab key is pressed. Each line of code should be no longer than 120 characters.

7. Methods and function calls

There cannot be a space between the method name and the opening parentheses. The rules for parameter lists in method calls are the same as for parameter lists in method declarations. In other words, for single-line calls, there can be no spaces after the opening parenthesis or before the closing parenthesis. Each parameter should be followed by a comma and there should be a space before the next parameter. If a method call requires multiple lines of code, each parameter should be on its own line and indented, and the closing parenthesis should be on its own line.

$earthGanme = new EarthGame( 
     5,  
     'earth',
      true,
      true
 );
$earthGame::generateTile(5, true);

8. Process control

Process control keywords (if, for, while, etc.) must be followed by a space. However, there cannot be a space after the opening parenthesis. Likewise, there can be no spaces before the closing parenthesis. Therefore the content should fit snugly within the brackets. In contrast to class and (single-line) function declarations, the opening curly brace of flow control code should be on the same line as the closing parenthesis. The closing curly brace should be on a line of its own. Here's a simple example.

$title = [];
for ($x = 0; $x < $diamondCount; $x++) { 
   if ($polluted) {
        $title[] = new PollutionDecorator(new DiamondDecorator(new Plains()));
    } else {
        $title[] = new DiamondDecorator(new Plains());
    }
}
Basic coding specifications About basic specifications such as PHP tags and basic naming conventions
Coding style specifications Regulations on the position of braces and parameter lists and other coding formats
Log interface specification Provisions on log level and log recording behavior
Auto-loading specification Naming conventions for classes and namespaces, and provisions for mapping between them and file systems
Cache interface specification Regulations on cache management, including data types, cache item life cycle, error handling, etc.
HTTP message interface specification Conventions about HTTP requests and responses

The above is the detailed content of PHP coding style specification detailed introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete