作家
登录

PHP代码简洁之道——SOLID原则

作者: 来源: 2017-10-25 09:04:08 阅读 我要评论

 showList($employees){     
  • foreach ($employees as $employee) { 
  •         $expectedSalary =  
  • $employee->calculateExpectedSalary(); 
  •         $experience = $employee->getExperience(); 
  •         $githubLink = $employee->getGithubLink(); 
  •         $data = [ 
  •             $expectedSalary, 
  •             $experience, 
  •             $githubLink 
  •         ]; 
  •  
  •         render($data); 
  •     } 
  •  
  • Good:

    对这个概念最好的解释是:如不雅你有一个父类和一个子类,在不改变原有结不雅精确性的前提下父类和子类可以交换。这个听起来让人有些困惑,所以让我们来看一个经典的┞俘方形-长方形的例子。大年夜数学上讲,正方形是一种长方形,然则当你的模型经由过程持续应用了"is-a"的关系瓯,就纰谬了。

    Bad:

    1. class Rectangle{     
    2.     protected $width = 0;     
    3.     protected $height = 0;     
    4.     public function render($area) 
    5.     {        // ... 
    6.     }     
    7.     public function setWidth($width) 
    8.     {        $this->width = $width; 
    9.     }     
    10.     public function setHeight($height) 
    11.     {        $this->height = $height; 
    12.     }     
    13.     public function getArea() 
    14.     {        return $this->width * $this->height; 
    15.     } 
    16. class Square extends Rectangle{     
    17.     public function setWidth($width) 
    18.     {         
    19.         $this->width = $this->height = $width; 
    20.     }     
    21.     public function setHeight(height) 
    22.     {        $this->width = $this->height = $height; 
    23.     } 
    24. function

        推荐阅读

        微服务架构中模块划分和服务识别

      沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 比来在进行微办事架构的交换和>>>详细阅读


      本文标题:PHP代码简洁之道——SOLID原则

      地址:http://www.17bianji.com/lsqh/38154.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)