命名规范
Θ 类文件都以.class.php为后缀,使用驼峰法命名,并且首字母大写,例如 Pay.class.php;
Θ 类名和目录_文件名一致。例如:类名Zend_Autoloader的目录是Zend/Autoloader.class.php;
Θ 函数的命名使用小写字母和下划线的方式。例如:get_client_ip;
Θ 方法的命名使用驼峰法,首字母小写或者使用下划线"_",例如listComment(),_getResource(),通常下划线开头的方法属于私有方法;
Θ 属性的命名使用驼峰法,首字母小写或者使用下划线"_",如$username,$_instance,通常下划线开头的属性属于私有属性;
Θ 常量以大写字母和下划线"_"命名,如"HOME_URL";
常用名词
1>list名词(单数),如listApple,一看我们就知道读取苹果列表,我们没有必要写成getApples或者listApples或readApples——因为get我们规定一般用于读取单个数据,如getApple.listApples不加s我们也知道是取苹果列表(保证尽量缩短变量命名);
2>get名词(单数);
3>名词Total,表示某个东西的总数。如expenseTotal;
4>found:表示某个值是否已经找到;
5>uccess或ok:一项操作是否成功;
6>done:某个工程是否完成;
7>error:是否有错误发生;
8>result:返回的结果
代码重构
1.函数或者方法体内的代码尽量控制在一个屏幕内。
2.类中不使用的方法随机删除。
3.修改别人的类中方法,要签名。
4.在每个模块内写个readme文件(用于比较复杂业务的说明或代码说明)。
5.尽量让每个类做自己的事,每个函数做一件事。
常用代码
用&&或||简化操作
简化前:
复制代码 代码如下:
$a=1;
$b = 0;
if(isset($a)){
$b=1;
print($b."\n");
}
if($b!=0){
print($b."\n");
}
简化后:
复制代码 代码如下:
$a=1;
$b = 0;
isset($a) && ($b=1) && print($b."\n");
$b == 0 || print($b."\n");
明显代码看起来更加整齐,更加简单!
判断"=="时,把常量放在前面
之前:
复制代码 代码如下:
$a = 1;
if($a = 1){
echo '$a == 1';
}
之后:
复制代码 代码如下:
$a = 1;
if(1 = $a){
echo '$a == 1';
}
明显,常量放在前面的话,编译器就能判断错误。
正规格式:
复制代码 代码如下:
$a = 1;
if(1 == $a){
echo '$a == 1';
}
查找表法
之前:
复制代码 代码如下:
/*错误码:4,5,7,8的时候返回状态1,错误码是1,3,6返回状态2*/
$error = 4;
$state = 0;
if($error == 4 || $error == 5 || $error == 7 || $error == 8){
$state = 1;
}
if($error == 1 || $error == 3 || $error == 6){
$state = 2;
}
echo "$state \n";
之后:
复制代码 代码如下:
/*错误码:4,5,7,8的时候返回状态1,错误码是1,3,6返回状态2*/
$error = 4;
$state = 0;
$arr = array(4 => 1, 5 => 1, 7 => 1, 8 => 1, 1 => 2, 3 => 2, 6 => 2);
isset($arr[$error]) && ($state = $arr[$error]);
echo "$state \n";
明显代码更加凝练,更加清楚,更易懂,速度也更快!
总结
本来想把什么设计模式也往常用代码里放置,但是太多了,不太好放。这些只是微部而已!
大家如果有更好的写法的话,可以留言。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft