The basic agreement

The source file

Code using

The file must be in UTF-8 format without BOM

A file declares only one type, such as class and interface cannot be mixed in one source file

The indentation

Use 4 Spaces for indentation, which the IDE can set

Governor degrees

Each line contains 120 characters

The keyword

All keywords are lowercase, such as true and false

named

Class name is big hump method, such as UserModel

Class methods are called small hump methods, such as getUserId()

Functions use lowercase letters plus _, such as get_cookie()

Use the small hump method for variable names such as $userId

Constants are defined as uppercase plus _ combinations, such as IS_DEBUG

Code comment tag

To annotate classes, methods, and properties in class files, use @param@return @throwns

For example, @param string $username specifies the username

Business module

The route consists of lowercase letters plus _, for example, / API /get_user_info

The View layer is responsible for data presentation

The Controller layer is responsible for checking input parameters, and the outermost layer catches exceptions and calls the Logic and View layers

The Logic layer is responsible for the concrete business Logic and calls the Model layer to return processing data

The Model layer is responsible for data table queries and associations

For example, ParamException indicates a parameter error, and UserException indicates a user-defined exception

Exceptions need to be defined by class code, using PHP class constants instead, as in

<? php namespace app\exceptions\codes; class UserExceptionCode extends BaseExceptionCode { const NO_AUTH = 1000001; Const NO_AUTH_MSG = 'no permission '; const STATUS_EXCEPTION = 1000002; Const STATUS_EXCEPTION_MSG = 'abnormal state '; }Copy the code

Table files with Enum types are replaced with PHP class constants, such as

<? php <? php namespace app\enums; class UserEnum extends BaseEnum { const STATUS_DELETED = -1; // const STATUS_DISABLE = 0; // Disable const STATUS_ENABLE = 1; // normal const AUTH_GUEST = 1; // Anonymous user const AUTH_GENERAL_ADMIN = 2; // Const AUTH_SUPER_ADMIN = 3; // Super administrator}Copy the code

Where STATUS and AUTH are the names of data table mapping fields

Api interface output, example

{
  "code" : 0,
  "msg" : "success",
  "data" : {
    "userId" : 100
  }
}
Copy the code

Where code and MSG are mandatory fields and data is empty

{"code" : 100001, "MSG" : "no permissions"}Copy the code

other

Array, using single quotes when the key is a string and a single line when the key is only one, example

$arr = [ 'userId' => 100 ];

Copy the code

Multiple lines for multiple keys, example

$arr = [
  'id'    => 100,
  'username' => 'admin',
];
Copy the code

There are many, many specifications, you can go to wechat search: PHP open source community to find some learning!

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you