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

What are the PHP development specifications?

王林
Release: 2023-02-28 18:28:02
forward
2422 people have browsed it

What are the PHP development specifications?

Source file

The code starts with

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;;    
}
Copy after login

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;// 超级管理员
}
Copy after login

where STATUS and AUTH are the data table mapping field names

Api interface output , Example

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

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

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

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 ];
Copy after login

Use multiple lines when there are multiple keys, example

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

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template