💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 开发规范 #### 目录和文件 * 目录使用小写+下划线; * 类文件采用大驼峰法命名,其它文件采用小写+下划线命名; * 类名和类文件名保持一致,统一采用大驼峰法命名; #### 函数和类、属性命名 * 类的命名采用驼大峰法 , 例如 UserType * 函数的命名使用小写字母和下划线的方式,例如 get_client_ip * 方法的命名使用小驼峰法 , 例如 getUserName * 属性的命名使用驼峰法 , tableName #### 常量和配置 * 常量以大写字母和下划线命名,例如 APP_PATH; * 配置参数以小写字母和下划线命名,例如 url_route_on #### 数据表和字段 * 数据表和字段采用小写加下划线方式命名,并注意字段名不要以下划线开头,例如 tp_user 表和 user_name字段 * * * * * ## 高质量实用技巧 #### 1.不要使用相对路径 常常会看到: ~~~ require_once('../../lib/some_class.php'); ~~~ 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录。因此会检查过多路径。如果该脚本被另一目录的脚本包含, 它的基本目录变成了另一脚本所在的目录. 推荐写法: ~~~ define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME)); require_once(ROOT . '../../lib/some_class.php'); ~~~ #### 2. 不要直接使用 require, include, include_once, required_once 可以在脚本头部引入多个文件, 像类库, 工具文件和助手函数等, 如: ~~~ require_once('lib/Database.php'); require_once('lib/Mail.php'); require_once('helpers/utitlity_functions.php'); ~~~ 推荐写法: ~~~ function load_class($class_name) { $path = ROOT . '/lib/' . $class_name . '.php'); if(file_exists($path)) { require_once( $path ); } } ~~~ #### 3. 灵活编写函数 ~~~ function add_to_cart($item_id , $qty) { $_SESSION['cart']['item_id'] = $qty; } add_to_cart( 'IPHONE3' , 2 ); ~~~ 使用上面的函数添加单个项目. 而当添加项列表的时候,只要稍加留意不同类型的参数, 就会更灵活. 如: 推荐写法: ~~~ function add_to_cart($item_id , $qty) { if(!is_array($item_id)) { $_SESSION['cart']['item_id'] = $qty; } else { foreach($item_id as $i_id => $qty) { $_SESSION['cart']['i_id'] = $qty; } } } add_to_cart( 'IPHONE3' , 2 ); add_to_cart( array('IPHONE3' => 2 , 'IPAD' => 5) ); ~~~ #### 4. 写文件前, 检查目录写权限 ~~~ $contents = "All the content"; $file_path = "/var/www/project/content.txt"; file_put_contents($file_path , $contents); ~~~ 这大体上正确. 但有些间接的问题. file_put_contents 可能会由于几个原因失败: 1. 父目录不存在 2. 目录存在, 但不可写 3. 文件被写锁住? 所以写文件前做明确的检查更好. 推荐写法: ~~~ $contents = "All the content"; $dir = '/var/www/project'; $file_path = $dir . "/content.txt"; if(is_writable($dir)) { file_put_contents($file_path , $contents); } else { die("Directory $dir is not writable, or does not exist. Please check"); } ~~~ #### 5. 更改应用创建的文件权限 在linux环境中, 权限问题可能会浪费你很多时间. 从今往后, 无论何时, 当你创建一些文件后, 确保使用chmod设置正确权限 推荐写法: ~~~ chmod("/somedir/somefile", 0755); ~~~ #### 6. 將工具函数封装到类中 假如你在某文件中定义了很多工具函数: ~~~ function utility_a() { //This function does a utility thing like string processing } function utility_b() { //This function does nother utility thing like database processing } function utility_c() { //This function is ... } ~~~ 这些函数的使用分散到应用各处. 你可能想將他们封装到某个类中: 推荐写法: ~~~ class Utility { public static function utility_a() { } public static function utility_b() { } public static function utility_c() { } } //and call them as $a = Utility::utility_a(); $b = Utility::utility_b(); ~~~ 显而易见的好处是, 如果php内建有同名的函数, 这样可以避免冲突. 另一种看法是, 你可以在同个应用中为同个类维护多个版本, 而不导致冲突. 这是封装的基本好处, 无它. #### 7.一些建议 ~~~ 1. 使用echo取代print 2. 使用str_replace取代preg_replace, 除非你绝对需要 3. 不要使用 short tag 4. 简单字符串用单引号取代双引号 5. head重定向后记得使用exit 6. 不要在循环中调用函数 7. isset比strlen快 8. 始中如一的格式化代码 9. 不要删除循环或者if-else的括号 10. 不要尝试省略一些语法来缩短代码. 而是让你的逻辑简短. ~~~ #### 8. 使用array_map快速处理数组 比如说你想 trim 数组中的所有元素. 新手可能会: ~~~ foreach($arr as $c => $v) { $arr[$c] = trim($v); } ~~~ 但使用 array_map 更简单: 推荐写法: ~~~ $arr = array_map('trim' , $arr); ~~~ 这会为$arr数组的每个元素都申请调用trim. 另一个类似的函数是 array_walk. 请查阅文档学习更多技巧. #### 9. 小心处理大数组 对于大的数组和字符串, 必须小心处理. 常见错误是发生数组拷贝导致内存溢出,抛出Fatal Error of Memory size 信息: 当导入或导出csv文件时, 常常会这么做。 推荐写法: 确保通过引用传递, 或存储在类变量中: ~~~ $a = get_large_array(); pass_to_function(&$a); ~~~ 这么做后, 向函数传递变量引用(而不是拷贝数组). 查看文档. ~~~ class A { function first() { $this->a = get_large_array(); $this->pass_to_function(); } function pass_to_function() { //process $this->a } } ~~~ 尽快的 unset 它们, 让内存得以释放,减轻脚本负担.