加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

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

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

优化方案2

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

案例3

(编辑:辽源站长网)

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

推荐文章
    热点阅读