1. Overview
- The code must follow the PSR-1 Basic code specification.
- The code must be indented with four space characters, not a TAB key.
- Each line should remain within 80 characters and the soft limit must be 120 characters, but must not have a hard limit.
- After each namespace namespace declaration statement and the use declaration statement block, there must be a blank line.
- The opening curly brace ({) of a class must be written as a line after the function declaration, and the closing curly brace (}) must also be written in a row after the function body.
- The opening curly brace ({) of the method must be written in a line after the function declaration, and the closing curly brace (}) must also be written in a row after the function body.
- Visibility must be declared on all properties and methods (Translator Note: private,protected and public); abstract and final must be declared before visibility; static must be declared after visibility.
- The key to the control structure must be a space character followed by a method and function call.
- The opening curly brace ({) of the control structure must be written on the same line as the declaration, and the closing curly brace (}) must be written in a row after the body.
- You must not have a space after the opening opening parenthesis ({) of the control structure, and must not have spaces before the closing parenthesis (}).
1.1 Examples
The following example simply shows most of the above specifications.
<?php namespace Vendor\Package; use FooInterface; use BarClass as Bar; use OtherVerdor\OtherPackage\BazClass; class Foo extends Bar implements FooInterface { public function sampleFunction() { if ( $a === $b ){ bar(); } elseif ( $a > $b ){ } else { } } } ?>
2. General principles 2.1 Basic Coding specification
Code must conform to all specifications of PSR-1
2.2 Files
All PHP files must use the Unix LF (linefeed) as the line terminator.
All PHP files must end with a blank line.
The pure PHP code file must omit the last?> end tag.
2.3 Rows
The length of the line must not be rigidly constrained.
The soft constraints of the length of the line must be limited to 120 characters or less, and the Code spec Checker must warn you if it exceeds this size and must not give an error.
Each line should not exceed 80 characters, and a line that is more than 80 characters should be split into successive lines of no more than 80 characters.
There must be no extra whitespace at the back of a non-blank line.
Blank lines can be used to improve readability and to differentiate between related blocks of code.
Each line must not have an extra statement.
2.4 Indent
The code must be indented with four spaces and must not use the TAB key.
2.5 Keywords and true/false/null
The PHP keyword must be all lowercase.
PHP constants True,false and null must all be lowercase.
3. Namespaces and use declarations
When a namespace exists, it must be followed by a blank line.
All use must be declared after the namespace.
Each use declaration statement must have only one use keyword.
You must have a blank line after the use declaration statement block.
For example:
<?php namesapce Vendor\Package; use FooClass; //
4. Classes, properties, and methods
The "class" Here refers to all class classes, interfaces, and traits reusable blocks of code.
4.1 Extensions and inheritance
Keywords extends and implements must be on the same line as the class name.
The curly braces ({) At the beginning of the class must be exclusive, and the curly braces (}) at the end of the class must be exclusive to the class body.
<?phpnamespace Vendor\Package;use FooClass;use BarClass as Bar;use OtherVendor\OtherPackage\BazClass;class ClassName extends ParentClass implements \ArrayAccess, \Countable{ // constants, properties, methods}
The Implements interface list can also be divided into multiple lines, and when doing so, the first interface in the list must be placed on the next row, each line must have only one interface
<?phpnamespace Vendor\Package;use FooClass;use BarClass as Bar;use OtherVendor\OtherPackage\BazClass;class ClassName extends ParentClass implements \ArrayAccess, \Countable, \Serializable{ // constants, properties, methods}
4.2 Properties
All properties must declare visibility.
You must not declare a property with the keyword var.
Each statement must not have more than one attribute defined.
Property names should not be prefixed with a single underscore to indicate that the property is protected or private.
The declaration of a property should look like this.
<?phpnamespace Vendor\Package;class ClassName{ public $foo = null;}
4.3 methods
All methods must declare the visibility (access modifier).
The method name should not be prefixed with a single underscore as protected or private.
The method name must not have a space character after it, its opening curly braces have to be exclusive, and the closing curly braces must also be separated into a line after the method body. The argument must not have spaces before the left parenthesis and the closing parenthesis.
A method definition should look like the following, noting the position of parentheses, commas, spaces, and braces:
<?phpnamespace Vendor\Package;class ClassName{ public function fooBarBaz($arg1, &$arg2, $arg3 = []) { // method body }}
4.4 Method Parameters
In the argument list, there must be no spaces before each comma, and a space after each comma.
The method parameter, which has a default value, must be placed at the end of the parameter list.
<?phpnamespace Vendor\Package;class ClassName{ public function foo($arg1, &$arg2, $arg3 = []) { // method body }}
The argument list can be split into multiple lines with one indent at a time, so that the first item in the list must be placed on the next line, and each row must have only one parameter.
After splitting the argument list into multiple rows, the closing parenthesis and the method start curly braces must be written on the same line, separated by a space, and in a row.
<?phpnamespace Vendor\Package;class ClassName{ public function aVeryLongMethodName( ClassTypeHint $arg1, &$arg2, array $arg3 = [] ) { // method body }}
4.5 Abstract,final and static
Abstract and final declarations must be placed before visibility when they exist.
When present, the static declaration must be placed after visibility.
<?phpnamespace Vendor\Package;abstract class ClassName{ protected static $foo; abstract protected function zim(); final public static function bar() { // method body }}
4.6 Methods and function calls
Method and function call, there must be no space between the method name or the function name and the opening parenthesis, and there must be no spaces before the argument closing parenthesis. In the argument list, there must be no spaces before each comma, and a space after each comma.
<?phpbar();$foo->bar($arg1);Foo::bar($arg2, $arg3);
Parameters can be split into successive rows with one indent, and if you do, the first one in the argument list must be placed on the next line, and each row can have only one parameter.
<?php$foo->bar( $longArgument, $longerArgument, $muchLongerArgument);
5. Control structure
- The basic style specification for the control structure is as follows:
- Control structure keyword must have a space.
- Opening parenthesis (must not have spaces at the back).
- Must not have a space before the closing parenthesis).
- The closing parenthesis) and the opening curly brace {must have a space between them.
- The struct must be indented one time.
- Closing curly braces} must be self-formed after the body.
The subject of each struct must be enclosed in paired curly braces, which makes the structure look more canonical and reduces the likelihood of errors when adding new rows.
5.1 If,elseif,else
The structure of an if is shown below. Notice the brackets, spaces, and the position of the curly braces, and else and the ElseIf on the same line as the closing curly brackets of the previous body.
<?phpif ($expr1) { // if body} elseif ($expr2) { // elseif body} else { // else body;}
You should use ElseIf instead of else if, so that all control keywords look like a word.
5.2 Switch and case
A switch structure is shown below. Note the brackets, spaces, and the position of the curly braces. The case statement must be indented relative to switch, and the break keyword (or other terminating keyword) must be indented with the case body. If there is a non-empty case straight-through statement, you must have a comment like//no break.
<?phpswitch ($expr) { case 0: echo ‘First case, with a break‘; break; case 1: echo ‘Second case, which falls through‘; // no break case 2: case 3: case 4: echo ‘Third case, return instead of break‘; return; default: echo ‘Default case‘; break;}```### 5.3 while,do while一个while语句如下所示。注意其括号、空格以及花括号的位置。```<?phpwhile ($expr) { // structure body}```同样,一个do while语句如下所示。注意其括号、空格以及花括号的位置。```<?phpdo { // structure body;} while ($expr);```### 5.4 for一个for语句看起来像下面这个样子。注意其括号、空格以及花括号的位置。
<?php
for ($i = 0; $i < $i + +) {
For body
}
### 5.5 foreach一个foreach语句看起来像下面这样。注意其括号、空格以及花括号的位置。
<?php
foreach ($iterable as $key = = $value) {
foreach Body
}
### 5.6 try,catch
<?php
try {
Try Body
} catch (Firstexceptiontype $e) {
Catch body
} catch (Otherexceptiontype $e) {
Catch body
}```
PHP Daily Progress A little bit of code specification PSR-2