- abstract class Shape{
- protected $width = 0;
- protected $height = 0;
- abstract public function getArea();
- public function render($area) { // ...
- }
- }
- class Rectangle extends Shape{
- public function setWidth($width)
- { $this->width = $width;
- }
- public function setHeight($height)
- { $this->height = $height;
- }
- public function getArea()
- { return $this->width * $this->height;
- }
- }
- class Square extends Shape{
- private $length = 0;
- public function setLength($length)
- { $this->length = $length;
- }
- public function getArea()
- { return pow($this->length, 2);
- }
- }
- function renderLargeRectangles($rectangles){
- foreach ($rectangles
推荐阅读
沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 比来在进行微办事架构的交换和>>>详细阅读
本文标题:PHP代码简洁之道——SOLID原则
地址:http://www.17bianji.com/lsqh/38154.html
1/2 1
PHP代码简洁之道——SOLID原则
作者: 来源: 2017-10-25 09:04:08 阅读次 我要评论
renderLargeRectangles($rectangles){ foreach ($rectangles as $rectangle) { $rectangle->setWidth(4); $rectangle->setHeight(5); $area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20. $rectangle->render($area); } } $rectangles = [new Rectangle(), new Rectangle(), new Square()]; renderLargeRectangles($rectangles);
关键词: 探索发现
乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。



