记录PHP编码规范
来源:互联网 发布:淘宝app里面是h5吗 编辑:程序博客网 时间:2024/06/08 03:39
PHP编码规范
版权声明
每一个文件包含版权声明信息,具体格式如下
/** * 一行说明本文件的功能 * * 如有必要,请详细说明本文件的具体内容和实现,可以多行。 * * @category Controller|Service|Template|Model|Middleware * @package 包名称 * @author 张三 <zhangsan@-inc.com> * @copyright 2019 (C) 某某科技有限公司 * @version SVN: $Id$ * @since v1.0 引入文件时 * @deprecated v1.1 弃用了本文件 */
本编码规范大部分遵循PSR-2中的规范
基本
- 所有PHP文件 必须 使用 Unix LF (linefeed) 作为行的结束符,且文件以UTF-8编码无BOM格式保存。
- 所有PHP文件 必须 以一个空白行作为结束。
- 纯PHP代码文件 必须 省略最后的 ?> 结束标签。
- 每一行末尾不许有空格和tab的空白字符。
- 每一行尽可能不超过80个字符,但绝对不可以超过120个字符。
- 四个空格的方式缩进,不允许使用tab进行缩进
- PHP所有 关键字 必须 全部小写。常量 true 、false 和 null 也 必须 全部小写。
命名规范
- 常量 Const:全部大写,单词之间使用下划线(
_
)连接,如PHP_CONST_VAR
- 类 Class: 采用单词首字母大写的驼峰命名,中间无空格和连接符的方式,如
ClassNameExample
- 方法 Method: 采用首单词小写开头驼峰命名,第二及之后的单词首字母大写,如
methodNameExample
- 属性 Property: 全部采用小写,单词直接使用下划线(
_
),下划线分隔式,如class_property_example
Laravel 项目中的规范
- Controller中的方法,尽可能使用
Requests
文件的方式来定义请求权限和验证,但都必须使用(Request $request)
参数接收HTTP Request提交的内容。 - 代码中不允许使用
$_GET
和$_POST
接受HTTP Request的提交的内容。 - 设计模式上要严格遵从Laravel的MVC框架,避免直接的
DB::table
操作 - 涉及到对
Model
数据更新的操作应该走仓库设计模式Repository
代码注释
- PHP项目中全部使用PHPDoc的编写方式,例子
class DateTimeHelper{ /** * @param mixed $anything Anything that we can convert to a \DateTime object * * @throws \InvalidArgumentException * * @return \DateTime */ public function dateTimeFromAnything($anything) { $type = gettype($anything); switch ($type) { // Some code that tries to return a \DateTime object } throw new \InvalidArgumentException( "Failed Converting param of type '{$type}' to DateTime object" ); } /** * @param mixed $date Anything that we can convert to a \DateTime object * * @return void */ public function printISO8601Date($date) { echo $this->dateTimeFromAnything($date)->format('c'); } /** * @param mixed $date Anything that we can convert to a \DateTime object */ public function printRFC2822Date($date) { echo $this->dateTimeFromAnything($date)->format('r'); }}
- 在涉及到接口输出的部分,使用ApiDoc的方式写代码注释
/** * @api {get} /user/:id Request User information * @apiName GetUser * @apiGroup User * * @apiParam {Number} id Users unique ID. * * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */
- apidoc和PHPDoc可以同时编写
阅读全文
0 0
- 记录PHP编码规范
- Android编码规范记录
- PSR规范--php编码规范
- PHP程序编码规范
- PHP 编码规范
- PHP 编码规范
- PHP 编码规范
- PHP 编码规范
- PHP编码规范
- PHP 编码规范1
- PHP 编码规范2
- PHP 编码规范3
- PHP 编码规范4
- PHP 编码规范5
- PHP 编码规范6
- PHP 编码规范
- PHP 编码规范7
- PHP 编码规范8
- 阿里云https配置
- 用pandas按列合并两个文件
- mysql(三):mysql查询语句 和 多表关联查询 以及 子查询
- 数据分析
- 7.0权限处理
- 记录PHP编码规范
- wamp下开启https双向验证
- Mac OS 使用笔记
- git
- Revit调用winform
- string的正则表达式
- flask1
- mysql innodb 行锁解锁后出现1213 死表现象 Deadlock found when
- guava库