Home >Backend Development >PHP Tutorial >Detailed explanation of PHP development specification examples
This article mainly shares with you detailed examples of PHP development specifications, hoping to help everyone.
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.
Use 4 spaces to indent, IDE can set
120 characters per line
All keywords are lowercase, such as true, false
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
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
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 = '不具有权限'; const STATUS_EXCEPTION = 1000002; const STATUS_EXCEPTION_MSG = '状态异常'; }
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;// 超级管理员 }
Where STATUS and AUTH are the data table mapping field names
Api interface output, example
##
{ "code" : 0, "msg" : "success", "data" : { "userId" : 100 } }where code and msg are required fields, Do not fill in if data is empty, example
{ "code" : 100001, "msg" : "不具有权限" }Other
$arr = [ 'userId' => 100 ];Use multiple lines when using multiple keys, use a single line when using strings like
$arr = [ 'id' => 100, 'username' => 'admin', ];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!