Good:
对这个概念最好的解释是:如不雅你有一个父类和一个子类,在不改变原有结不雅精确性的前提下父类和子类可以交换。这个听起来让人有些困惑,所以让我们来看一个经典的┞俘方形-长方形的例子。大年夜数学上讲,正方形是一种长方形,然则当你的模型经由过程持续应用了"is-a"的关系瓯,就纰谬了。
Bad:
- class Rectangle{
- protected $width = 0;
- protected $height = 0;
- public function render($area)
- { // ...
- }
- 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 Rectangle{
- public function setWidth($width)
- {
- $this->width = $this->height = $width;
- }
- public function setHeight(height)
- { $this->width = $this->height = $height;
- }
- }
- function
推荐阅读
沙龙晃荡 | 去哪儿、陌陌、ThoughtWorks在主动化运维中的实践!10.28不见不散! 比来在进行微办事架构的交换和>>>详细阅读
本文标题:PHP代码简洁之道——SOLID原则
地址:http://www.17bianji.com/lsqh/38154.html
1/2 1