Home > Article > Backend Development > PHP coding standards_PHP tutorial
1. File format
1. For files containing only php code, we will ignore the "?>" at the end of the file. This is to prevent extra spaces or other characters from affecting the code.
For example:
$foo = 'foo';
2. Indentation should be able to reflect the logical results of the code. Try to use four spaces and prohibit the use of the tab character TAB, because this can ensure the flexibility of cross-client programmer software.
For example:
if (1 == $x) {
$indented_code = 1;
if (1 == $new_line) {
$more_indented_code = 1;
}
}
3. Variable assignments must be equally spaced and arranged.
For example:
$variable = 'demo';
$var = 'demo2';
4. The length of each line of code should be controlled within 80 characters, and the maximum length should not exceed 120 characters. Because Linux generally reads files in units of 80 columns, that is to say, if a line of code exceeds 80 characters, the system will pay additional operating instructions for this. Although this seems like a minor issue, it is also a norm that should be noted and followed by programmers who pursue perfection.
5. No extra spaces are allowed at the end of each line.
2. Naming Convention
1. Class files are all suffixed with ".class.php", and class file names only allow letters, use camel case naming, and the first letter is capitalized, for example: DbMysql.class.php.
2. Files other than configuration and function library files are generally suffixed with ".inc.php" and ".php" respectively, and the file names use lowercase letters and underscores, with multiple words separated by Separated by underscores, such as config.inc.php, common.php, install_function.php.
3. Make sure the file naming and calling case are consistent because Unix-like systems are case-sensitive.
4. The class name and the file name are consistent (including the upper and lower case mentioned above), and only letters are allowed in the class name. For example, the file name of the UserAction class is UserAction.class.php, and the file name of the InfoModel class is InfoModel.class.php.
5. Controller classes are suffixed with Action, such as UserAction, InfoAction, model classes are suffixed with Model, such as UserModel, InfoModel, and other classes are also suffixed with corresponding categories, such as Service and Widget.
6. Method names are only allowed to be composed of letters. Underlines are not allowed. The first letter must be lowercase, and the first letter of each subsequent word must be capitalized. This is the so-called "camel case naming" rule. The more detailed the better, it should be able to describe Be clear about the function of this method, such as switchModel and findPage.
7. Attribute names are only allowed to consist of letters. Underlines are not allowed. The first letter must be lowercase, and the first letter of each subsequent word must be uppercase. This is the so-called "camel case naming" rule, such as tablePrefix and tableName.
8. For accessing object members, we must always use “get” and “set” methods. For example:
class Foo
{
protected $_testObj;
public function getTestObj()
{
// ...
}
}
11. Class member attributes declared as private must start with a double underscore "__"; class member attributes declared as protected must start with an underscore "_"; and member attributes declared as public must not be used at any time. Underscores are allowed.
12. Use lowercase letters and underscores to name functions, and the more detailed the better, it should be able to clearly describe the function of the function, such as get_client_ip.
13. When a method or function parameter does not necessarily need to be assigned a value, use "null" instead of "false" as the default value of the function parameter, unless the parameter is a boolean value.
14. Variables are only allowed to consist of lowercase letters and underscores, and it is recommended to use descriptive variable names, the more detailed the better, so that the use of $i or $n, etc. is discouraged.
15. Constants in classes and global constants define can only be composed of uppercase letters and underscores, and each word is separated by underscores.
16. Both boolean and null values are lowercase.
3. Coding style
1. PHP code must be delimited in complete form (), that is, do not use PHP short tags (), and ensure there is no space after the closing tag.
2. When a string is composed of plain text (that is, it does not contain variables), it must always use single quotes (') as the delimiter. For example:
$a = 'Example String';
3. Variables in variable substitution are only allowed to be in the form of $ variable name. For example:
$greeting = "Hello $name, welcome back!"; // Allow
$greeting = "Hello {$name}, welcome back!"; // Allow
$greeting = "Hello ${name}, welcome back!"; // Not allowed
When concatenating strings with the dot ".", the string must be separated from the dot by a space, and it is allowed to be split into multiple lines to enhance readability. In this case, the period "." must be aligned with the equals sign "=". For example:
$sql = "SELECT `id`, `name` " . " FROM `people` "
. "WHERE `name` = 'Susan' "
. "ORDER BY `name` ASC ";
When constructing an array using the array type notation, a space must be added after each comma to enhance readability. For example: $sampleArray = array(1, 2, 3, 'Think', 'SNS');
4. When using the array type specifier to declare an associative array, we encourage dividing it into multiple rows, but we must also ensure that the keys and values of each row are aligned to maintain beauty. For example:
$sampleArray = array(
'firstKey' => 'firstValue',
'secondKey' => 'secondValue'
);
5. The beginning of the curly brace must be at the top of the next line of the class name. For example:
class Think
{
// ...
}
6. All code in a class must be indented with four spaces.
7. Only one class is allowed to be declared in each php file. Writing other code inside class files is allowed, but discouraged. If you really need to append code, it must be separated by blank lines.
8. Any declaration of class variables must be placed at the top of the class, before any declaration of functions.
9. The var symbol is not allowed to be used to declare variables. Class member variables must be declared as private, protected and public. Second, although it is allowed to declare class members as public and reference them directly, it is usually better to use the get and set methods to access class members.
10. Methods must always declare their scope using private, protected or public.
11. Static static methods should declare their scope, and should no longer be declared as private, but should be protected or public. If they just do not want to be inherited by subclasses, they should be declared final.
12. The initial curly brace of a function or method should be at the top of the line following the function declaration. For example:
function get_client_ip()
{
// …
}
13. No extra spaces are allowed between the function or method name and the parameter brackets. For example:
function get_client_ip()
{
// …
}
14. References are only allowed to be defined in function parameters, and passing references in real time is prohibited. For example:
// References defined in function parameters - allowed
function defineRefInMethod(&$a)
{
$a = 'a';
}
defineRefInMethod($b);
echo $b; // 'a'
// Passing references in real time - forbidden
function callTimePassRef($a)
{
$a = 'a';
}
callTimePassRef(&$c);
echo $c; // 'a'
15. The return value of a function or method cannot be enclosed in parentheses, otherwise it will reduce readability, and if the function is modified to return a reference in the future, an exception will be thrown.
16. Encourage the use of type hints whenever possible, especially in module design. For example:
class Foo
{
public function foo(SomeInterface $object)