加入收藏 | 设为首页 | 会员中心 | 我要投稿 常州站长网 (https://www.0519zz.cn/)- 云渲染、网络安全、数据安全、数据分析、人体识别!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

如何成为一名优秀的工程师(语义篇)

发布时间:2019-03-06 22:30:06 所属栏目:优化 来源:佚名
导读:好的语义表达是团队协作中高效迭代的润滑剂,好的语义表达是线上未知代码问题排查的指南针。 本篇文章巨长,如果你比较懒,来我讲给你听(直播中有更多细节) 回放地址 看完这个还不过瘾?学习使你快乐?还想学习?快上车 不要让其他人读不懂你的代码,其他人可

优化方案2

  1. class Db 
  2. { 
  3.     /** 
  4.      * @param string $table 数据库表名 
  5.      * @param Entity $data  新增对象 
  6.      * 
  7.      * @return int 新增主键 
  8.      */ 
  9.     public static function insert(string $table, Entity $data) 
  10.     { 
  11.         $array = $data->toArray(); 
  12.         var_export($array); // test 
  13.  
  14.         $id = mt_rand(1, 1000); 
  15.         return $id; 
  16.     } 
  17. } 
  18.  
  19. class ArrayUtils 
  20. { 
  21.     /** 
  22.      * 针对成员都是私有属性的对象 
  23.      * 
  24.      * @param      $obj 
  25.      * @param bool $removeNull 去掉空值 
  26.      * @param bool $camelCase 
  27.      * 
  28.      * @return array 
  29.      */ 
  30.     public static function Obj2Array($obj, $removeNull = true, $camelCase = true) 
  31.     { 
  32.         $reflect = new ReflectionClass($obj); 
  33.         $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED); 
  34.  
  35.         $array = []; 
  36.         foreach ($props as $prop) { 
  37.             $prop->setAccessible(true); 
  38.             $key = $prop->getName(); 
  39.  
  40.             // 如果不是驼峰命名方式,就把对象里面的 createTime 转成 create_time 
  41.             if (!$camelCase) { 
  42.                 $key = preg_replace_callback("/[A-Z]/", function ($matches) { 
  43.                     return "_" . strtolower($matches[0]); 
  44.                 }, $key); 
  45.                 $key = ltrim($key, "_"); 
  46.             } 
  47.  
  48.             $value = $prop->getValue($obj); 
  49.  
  50.             if ($removeNull == true && $value === null) { 
  51.                 continue; 
  52.             } 
  53.  
  54.             if (is_object($value)) { 
  55.                 $value = self::Obj2Array($value); 
  56.             } 
  57.  
  58.             $array[$key] = $value; 
  59.         } 
  60.  
  61.         return $array; 
  62.     } 
  63. } 
  64.  
  65. class Entity 
  66. { 
  67.     public function toArray(){ 
  68.         return ArrayUtils::Obj2Array($this); 
  69.     } 
  70. } 
  71.  
  72. class ViewLogEntity extends Entity 
  73. { 
  74.     /** 
  75.      * @var int 
  76.      */ 
  77.     private $uid; 
  78.  
  79.     /** 
  80.      * @var string 
  81.      */ 
  82.     private $url; 
  83.  
  84.     /** 
  85.      * @var string 
  86.      */ 
  87.     private $referer; 
  88.  
  89.     /** 
  90.      * @var string 
  91.      */ 
  92.     private $createdTime; 
  93.  
  94.     /** 
  95.      * @param int $uid 
  96.      */ 
  97.     public function setUid(int $uid) 
  98.     { 
  99.         $this->uid = $uid; 
  100.     } 
  101.  
  102.     /** 
  103.      * @param string $url 
  104.      */ 
  105.     public function setUrl(string $url) 
  106.     { 
  107.         $this->url = $url; 
  108.     } 
  109.  
  110.     /** 
  111.      * @param string $referer 
  112.      */ 
  113.     public function setReferer(string $referer) 
  114.     { 
  115.         $this->referer = $referer; 
  116.     } 
  117.  
  118.     /** 
  119.      * @param string $createdTime 
  120.      */ 
  121.     public function setCreatedTime(string $createdTime) 
  122.     { 
  123.         $this->createdTime = $createdTime; 
  124.     } 
  125. } 
  126.  
  127.  
  128. class ViewLogStore 
  129. { 
  130.     private $table = "view_log"; 
  131.  
  132.     function record(ViewLogEntity $viewLogEntity) 
  133.     { 
  134.         Db::insert($this->table, $viewLogEntity); 
  135.     } 
  136. } 
  137.  
  138. // 测试 
  139.  
  140. $viewLogEntity = new ViewLogEntity(); 
  141. $viewLogEntity->setUid(1); 
  142. $viewLogEntity->setReferer("https://mengkang.net"); 
  143. $viewLogEntity->setUrl("https://segmentfault.com/l/1500000018225727"); 
  144. $viewLogEntity->setCreatedTime(date("Y-m-d H:i:s",time())); 
  145.  
  146. $viewLogStore = new ViewLogStore(); 
  147. $viewLogStore->record($viewLogEntity);  

案例3

(编辑:常州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读