Home Backend Development PHP Problem Basic specifications of PHP coding style

Basic specifications of PHP coding style

Aug 09, 2019 pm 05:54 PM
php coding style

Basic specifications of PHP coding style

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

Basic specifications of PHP coding style

##PSR-1 Basic Coding Specification

( 1) Opening and closing tags

First of all, the PHP code must start with the (2) Side effects

PHP files either declare classes, interfaces, functions, etc., or perform logical operations (such as reading and writing files or sending output to the browser), but they should not do both at the same time Both.

(3) Naming

The naming of the class must comply with the camel case naming convention starting with an uppercase letter. In other words, class names should start with a capital letter. There is no requirement to name properties, but they should be consistent. Method names must conform to the camelCase naming convention starting with lowercase. All letters in class constants must be capitalized, and words are separated by underscores.

Related recommendations: "

PHP Knowledge"

PSR-2 Coding Style Specification

(1) PSR -1 requires PHP code to start with 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

class keyword, class name, and 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 this

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

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

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

(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 the method should be written on its own line after the method name, and the closing curly brace (}) should also be written on the method The body should be on its own line (directly following 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 follows:

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

·Multi-line declaration

A single-line method declaration is not practical if the method has many parameters. 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
) {
  //方法体
}
Copy after login

(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);
Copy after login

(8) Process control

流程控制关键字(if、for、while等)后面必须紧跟一个空格。但是,开始圆括号后不能有空格。同样,结束圆括号前不能有空格。因此内容应该紧贴在括号内的。与类和(单行)函数声明相比,流程控制代码的开始花括号应该与结束圆括号在同一行。结束花括号应该自成一行。以下是一个简单的示例。

$title = [];
for ($x = 0; $x < $diamondCount; $x++) {
    if ($polluted) {
        $title[] = new PollutionDecorator(new DiamondDecorator(new Plains()));
    } else {
        $title[] = new DiamondDecorator(new Plains());
    }
}
Copy after login

注意if和for之后的空格。for和if语句与圆括号在同一行,而且它们的结束圆括号后都有一个空格,然后是流程控制体的开始花括号。

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles