Do you really understand the PHP coding style specification?
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, PSR-1 and PSR-2 in the PSR specification have been defined. There are some specifications in PHP coding. As long as we abide by these specifications, we can write very beautiful and neat code even if we use a flexible scripting language. First, let’s take a look at the PSR specifications that have been passed, and then briefly explain some specific requirements of the PSR-1 and PSR-2 specifications
Passed PSR:
PSR-1 basic coding specification:
1. Open and close Tags
First of all, the PHP code must start with a
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 not 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.
PSR2-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 statement, 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 this:
class EarthGame extends Game implements Playable, Savable { //类体 }
You can also 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 the 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 the method after the 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
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 ) { //方法体 }
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、方法与函数调用
方法名称和开始圆括号之间不能有空格。方法调用中的参数列表的规则与方法声明中的参数列表规则相同。换言之,对于单行调用,开始圆括号后或结束圆括号前不能有空格。每个参数之后应该紧跟一个逗号,下一个参数前应该有一个空格。如果需要使用多行代码进行方法调用,那么每个参数应该自成一行并缩进,而且结束圆括号也应该自成一行。
$earthGanme = new EarthGame( 5, 'earth', true, true ); $earthGame::generateTile(5, true);
8、流程控制
流程控制关键字(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()); } }
想了解更多相关问题请访问PHP中文网:PHP视频教程
The above is the detailed content of Do you really understand the PHP coding style specification?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Tips for dealing with Chinese garbled characters imported into Oracle. During the process of data import using Oracle database, we often encounter garbled Chinese data. This may be due to character set mismatch, data source encoding issues, or database configuration errors. In order to solve this problem, this article will share some tips for dealing with the problem of Chinese garbled characters imported into Oracle, so that everyone can smoothly import Chinese data in actual operations. 1. Check the database character set. Before dealing with the Chinese garbled problem, you first need to check

Python is a popular programming language that is becoming more and more popular in the field of software development. However, due to the characteristics of the Python language, some coding errors sometimes occur. This article will introduce some common Python coding errors and ways to avoid these errors, hoping to help developers write better Python code. Use the appropriate encoding method Python supports multiple encoding methods, such as UTF-8, UTF-16, GB2312, etc. When writing code, make sure you select the appropriate

Detailed explanation of Go language comment coding specifications In programming, comments are a very important coding specification, which can help other developers understand the meaning and logic of the code. Especially for team development, standardized comments can improve the readability and maintainability of the code. This article will introduce the comment coding specifications in the Go language in detail, and provide specific code examples to show how to write comments in a standardized way. 1. Single-line comments In Go language, single-line comments start with // and are followed by the comment content. Single-line comments are mainly used to comment on a certain line of code.

This article brings you relevant knowledge about PHP. It mainly talks about coding standards with you. It is also recommended that you try to follow the standards during development. Friends who are interested can take a look below. I hope it will be helpful to you. .

Discussion on the underlying development principles of PHP: Coding standards and best practices Introduction: PHP is a very popular server-side programming language, and its underlying development principles are very important for developers. This article will delve into the coding standards and best practices for underlying PHP development, and deepen understanding through code examples. 1. Coding standards and unified code style In the underlying development of PHP, it is very important to maintain the unity of code style, which can improve the readability and maintainability of the code. Use a common coding style specification, such as PSR-2,

When developing in C++, in addition to focusing on issues such as function implementation and performance optimization, developers also need to pay attention to the coding specifications of the code. Good coding practices not only improve code readability and maintainability, but also help reduce errors and increase code consistency. This article will introduce some common C++ development considerations to help developers avoid coding standard problems. Use meaningful naming: Variables, functions, and classes should be named to accurately reflect their purpose and functionality. Avoid using single letters or meaningless abbreviations as names

Introduction to Python's best practices in secure coding specifications With the rapid development of the network and the popularity of the Internet, secure coding has become a crucial link in software development. Along the way, developers need to use some best practices to ensure the code they write is secure. Python is a popular programming language that is widely used for web application and system development. In Python applications, developers need to pay attention to some common security issues and follow some secure coding practices to prevent potential

Understand the underlying development principles of PHP: Coding standards and best practice guidelines In today's web development field, PHP, as a widely used scripting language, is chosen as the tool of choice by many developers. However, for many PHP developers, understanding the underlying development principles of PHP and how to write standardized code can be a challenge. This article will introduce some basic principles of PHP underlying development, as well as coding standards and best practice guidelines to help readers improve the efficiency and quality of PHP development. 1. PHP underlying development principles PHP
