Weitere ähnliche Inhalte Ähnlich wie PHP Coding Standard and 50+ Programming Skills (20) PHP Coding Standard and 50+ Programming Skills2. 什么是 Good Code ?
1. It works
2. It keeps working
3. Any dev can understand why
4. 怎么样?
1. 从现在开始
2. 区别对待你的项目
5. 具体?
1. PHP 编程规范 _2012.doc
6. 1. 小项目:功能 > 代码规范
大项目:代码规范 > 功能
2. 显式的声明类名,例如:
$app = new $_REQUEST[ACTION];
可改为:
switch ($_REQUEST[ACTION]) {
case 'abcd':
$app = new abcd();
}
7. ip2long 在 32/64 位机子上表现不一致
使用字符串的优点:
a) 省却转换过程
b) 清晰,容易阅读
使用数字的优点:
a) 节省空间
b) 排序快,查询快
10. Restful 风格的编程
认证 ticket , cookie , oauth
json/xml
GET 、 POST 、 DELETE 、 PUT
统一而标准的 URI
完整的 API 文档
11. 再复杂的业务逻辑,都可以分解为 CURD
的组合
12. 经常统计的数字
Redis::incr() , Redis::decr()
13. 我们框架的 MVC
app – controller
include - model
15. 文档 /doc 集中放置,设置权限,避免外
部访问
18. XSS
Cookie 加 http-only – 微软提出的
过滤: strip_tags, htmlspecialchars, htmlentities
19. 防 SQL 注入
用 mysql_real_escape_string
Addslashes 不一定可靠
不省略单引号和双引号
SELECT * FROM album_info WHERE
album_id = ‘123’
20. MongoDB 注入
login.php?username=admin&passwd[$ne]=1
(int), (string), intval()
注意每个参数的类型
21. Iframe 跨域的 POST ,免刷新
<iframe name="add_favorite"
id="add_favorite"
src="http://www.56.com/domain.html"
marginwidth="0" marginheight="0"
frameborder="0" width="0" scrolling="no"
height="0"></iframe>
23. delete 和 update 操作都需要 limit 1
MongoDB 的拓展里面有相应选项
24. 不要使用 半中文半英文 或者 拼音 的命
名,例如 ding, cai, huodong
25. 设置 timezone
date_default_timezone_set (‘PRC’);
26. 使用 mktime ,而不是用 strtotime
integer 在 32 位和 64 位体系结构中长度是不
同的 . 因此某些函数如 strtotime 的行为会
不同
27. 设置 mysql 的 utf8 :
1. mysql_query("SET NAMES 'utf8'");
2. mysql_set_charset('utf8', $conn); 建议使用
3. mysqli_set_charset($conn, 'utf8'); 建议使用
28. $_SERVER[‘REQUEST_TIME’] 比 time()
更快,甚至更精确
29. unset 大数组 / 大变量
特别是在超内存的时候,要分 for while 去
执行
30. include() 和 require() 中使用绝对路径
require ROOT_DIR."include/class.WordFilter.php";
31. strtr 比 str_replace 快
string strtr ( string $str , string $from , string
$to )
32. 不使用纯函数编程
不使用复杂的 OOP ,简单的面向对象
不过度的重用
33. “@” - 掩耳盗铃
不得不用“ @”
34. echo $str1, $str2, $str3 更快
35. curl vs Http::Get/Post
If (!function_exists('curl_init')
36. 可多使用 类常量 const 和 static 方法
38. cookie 的 domain 和 path
最小化授权,带宽,安全
39. 判断是否 POST :
if ( strtoupper($_SERVER['REQUEST_METHOD']) ==
'POST‘)
{...}
40. 永远不嫌多的判断:
file_exists(), function_exists(), isset(), empty(),
is_writable(), is_*()
41. 使用 if (false !== strpos($string, "xxx")) , 而
不是 if(strpos($string, "xxx"))
42. header 后面接 exit 或者 die
44. xdebug 依然是强大的性能分析工具
45. 唯一的 database 连接
singleton
46. MongoDB
one collection per db
manual sharding
用尽量短的 key
47. 一个程序的起点至关重要
99% 的情形是,接手你程序的那个人会把
你的程序改的更烂
48. 不要暴露任何接口的特征,比如拓展名
/api/abc.php -> /api/abc/
49. 函数内保持一个,且唯一一个 return
50. 能不用 global 全局变量的就别使用
global vs $GLOBALS
51. 变量名,类名,函数名,方法名 都要描述
清楚这是做什么的,比如
$isUserLimit, importDataToTblUser()
52. 把所有 dirty 的东西集中在一个地方
54. 兼容所有浏览器的 <a> 标签 onclick 写
法:
<a id="abc" href="javascript:void(0);"
onclick="func(); return false;"></a>
55. 查询的时候,加了索引还不够,还得
LIMIT 1 ,特别是你确定只有一条,而你
也只需要一条记录的情况下
56. if ... else throw ... 是一个好习惯,至少你知
道应该在哪里加 log
57. 配置都写在 配置文件 里面,绝不要写在
代码中。
例如 cfg.all.inc.php ,最好只有一个配置文
件
58. URL seo :
http://site.douban.com/widget/biz_album/784
2139/photo/1517038635/
59. 不用的代码请注释掉,并注明 @deprecated
60. 优先使用 js 原生方法
document.getElementById 一定比
jQuery.find('#xxx') 要快
61. 一句话写 log : file_put_contents($logFile,
$logContent, FILE_APPEND);
62. 每一个数据结构和索引的设计,都要和主
管上级一起决定
对待缓存必须仔细,小心,谨慎
存在即合理
63. 单例 singleton 模式的应用,适合 file/db/
cache 等等。
经验:评论顶踩
64. 如何提高编程技巧?怎么开始?
从当前你接触到的代码开始,从每一个细
节开始,例如你手上的一个 bug ,一个函数
。
通过反复的修改,反复的优化,才能有所
提升。