Home  >  Article  >  Backend Development  >  What are the PHP development specifications?

What are the PHP development specifications?

王林
王林forward
2020-01-29 19:54:422378browse

What are the PHP development specifications?

Source file

The code starts with 9ee1886df220cdb2e6797799ed7577af

The file format must be BOM-free UTF-8 format

A file only declares one type. For example, class and interface cannot be mixed in one source file.

Indentation

Use 4 spaces for indentation. IDE can Settings

(recommended learning: php video tutorial)

Line length

120 characters per line

Keywords

All keywords are lowercase, such as true, false

Naming

The class name is big camel case, such as UserModel

The class method is named small camel case , such as getUserId()

The function uses a combination of lowercase letters plus _, such as get_cookie()

The variable name uses small camel case, such as $userId

The constant is defined as uppercase letters Add _ combination, such as IS_DEBUG

Code comment tag

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

@param annotation to write Detailed explanation, such as @param string $username Username

Business module

Routing is composed of lowercase letters plus _, such as /api/get_user_info

View layer is responsible for data display

The Controller layer is responsible for input parameter verification, and the outermost layer captures exceptions and calls the Logic and View layers.

The Logic layer is responsible for specific business logic, calls the Model layer, and returns processing data.

Model layer is responsible for data table query and association

Exception classes need to distinguish functions, such as ParamException indicates parameter error, UserException indicates custom exceptions

Exceptions need to be classified and defined code, use PHP class constants instead , such as

<?php
namespace app\exceptions\codes;

class UserExceptionCode extends BaseExceptionCode {
  const NO_AUTH       = 1000001;
  const NO_AUTH_MSG     = &#39;不具有权限&#39;;
  const STATUS_EXCEPTION   = 1000002;
  const STATUS_EXCEPTION_MSG = &#39;状态异常&#39;;    
}

If the data table file has an Enum type, use PHP class constants instead, such as

<?php
<?php
namespace app\enums;

class UserEnum extends BaseEnum {
  const STATUS_DELETED = -1;// 已删除
  const STATUS_DISABLE = 0;// 禁用
  const STATUS_ENABLE = 1;// 正常

  const AUTH_GUEST     = 1;// 匿名用户
  const AUTH_GENERAL_ADMIN = 2;// 普通管理员
  const AUTH_SUPER_ADMIN  = 3;// 超级管理员
}

where STATUS and AUTH are the data table mapping field names

Api interface output , Example

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

The code and msg are required fields, do not fill in if data is empty, Example

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

Others

Use when the key is a string array Single quotes, use a single line when there is only one key, example

$arr = [ &#39;userId&#39; => 100 ];

Use multiple lines when there are multiple keys, example

$arr = [  
&#39;id&#39;    => 100,  
&#39;username&#39; => &#39;admin&#39;,
];

Recommended related article tutorials: php tutorial

The above is the detailed content of What are the PHP development specifications?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Why should php use swooleNext article:Why should php use swoole