Detailed explanation of PHP development specification examples

小云云
Release: 2023-03-19 22:36:02
Original
2645 people have browsed it

This article mainly shares with you detailed examples of PHP development specifications, hoping to help everyone.

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 to indent, IDE can set

line length

120 characters per line

Keywords

All keywords are lowercase, such as true, false

Naming

The class name is in camel case, such as UserModel

Class method names are in CamelCase, such as getUserId()

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

Variable names use CamelCase Method, such as $userId

Constant is defined as a combination of uppercase letters plus _, such as IS_DEBUG

code comment label

in the class file Annotate classes, methods, and properties. Use @param @return @throwns

@param annotation to write detailed explanations, such as @param string $username username

Business module

Routes are composed of lowercase letters plus _, such as /api/get_user_info

The View layer is responsible for data display

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

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

The Model layer is responsible Data table query and association

Exception classes need to distinguish functions. For example, ParamException indicates parameter errors, UserException indicates custom exceptions

Exceptions need to be classified and defined code, use PHP classes Replace with constants, 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
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

where code and msg are required fields, Do not fill in if data is empty, example


{
  "code" : 100001,
  "msg" : "不具有权限"
}
Copy after login

Other


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


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

Use multiple lines when using multiple keys, use a single line when using strings like


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

Related recommendations:

php code PHP development specification manual PHP code specification detailed explanation

Summary of PHP development specification

Detailed Explanation of PHP Code Specifications in the PHP Development Specification Manual

The above is the detailed content of Detailed explanation of PHP development specification examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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