浅析php与数据库代码开发规范
1、PHP中对各类变量内容的命名规范
(1)目录命名、文件命名、局部变量命名:
使用英文名词、动词,以下划线作为单词的分隔,所有字母均使用小写
目录:upload、templates、install、manage……
文件:index.php 、 register.php 、 config.php……
变量:$user , $pay_time , $pay_del_cont ……
(2)全局常量命名:
使用英文名词、动词,所有字母都使用大写,以下划线分隔每个单词
define( 'WEBSITE_NAME', '名称' );
define( 'WEBSITE_URL', '地址' );
英文名词、动(3)数组变量命名:
词,以下划线作为分隔,所有字母均使用小写以array结束
$scope_array = array();
$book_id_array = array();
(4)对象变量命名:
英文名词、动词,以下划线作为分隔,可以完整采用类名或是简化类名,但是必须明确知道是什么类,所有字母均使用小写,在变量后加上_obj
$user_obj = new userAccount();
$pay_obj = new payOrder();
(5)类的命名:
使用英文名词,以大写字母作为词的分隔,其他的字母均使用小写,名词的首个字母使用小写,不使用下划线
class userAccount{ ……}
(6)方法命名:
使用英文名词、动词,以下划线作为单词的分隔,所有字母均使用小写
复制代码 代码如下:
class userAccount {
public $name_account=‘';
function is_account_ok(){
...
}
function add_account(){
...
}
}
关于对象属性的命名同理!
2、PHP中函数、符号、运算写作规范
(1)if语句大括号{}规则:
将大括号放置在关键词后方
使用IF语句尽量使用大括号
复制代码 代码如下:
if ( $condition ){
...
}else{
…
}
(2)、switch规则
每个case块结束处必须加上break,而default总应该存在处理未知情况,例:
复制代码 代码如下:
switch( $condition ){
case 'user':
...
break;
case 'type':
...
break;
default:
...
break;
}
(3)、声明定位规则
声明代码块需要对齐,且初次使用变量时需要初始化
$tableName = '';
$databaseObject = '';
尽量不使用以下方式,例:
$tableName;
$accuntName = '';
$databaseObject = '';
html的form表单各个元素名称与数据库字段尽量保持一致。
不要采用缺省方法测试非零值,必须显式测试,例:
if ( $name_pay_into != false ){
...
}else{
...
}
*尽可能使用单引号''而不是双引号"",在需要加入变量或是写sql语句除外。
*php文件中尽可能不出现html语句,如果实在无法解决尽量少用,考虑到模板的兼容性,
html文件中尽可能避免出现php语句。
*通常每个方法只执行一项逻辑动作事务,所以对它们的命名应该清楚的说明它们是做什么的:
用email_error_check()代替error_check()。
请注意命名不要与系统方法冲突。
3、PHP中各类注释规范
/**
* 分页预处理函数
* sql SQL语句
* page 当前页数
* limit 每页显示的数量
* maxs 查询总数
*/
function limit($sql,$page='0',$limit=10,$maxs=''){ }
//用户检测
if( $check_obj->username( $username ) == true ){ … }
$user_name = $_GET[user]; //获取用户信息
4、数据库设计与操作规范
数据库规范
数据库名称应该由概述项目内容的小写英文名词组成,以下划线分隔单词,
避免跨平台时可能出现的大小写错误。
数据表名称应该由物件对象名称的小写英文名词组成(尽可能对应系统中的业务类名称),以下划线分隔单词,避免跨平台时可能出现的大小写错误。
数据表的字段应避免使用varchar、text等不定长的类型,时间信息的字段使用int类型存储。
查询数据连接多表时各资源应该使用全名称,即tableName.fieldName,而不是fieldName。
SQL语句应尽可能符合ansi92标准,避免使用特定数据库对SQL语言的扩充特性。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
