简介
在PHP设计模式(二):抽象类和接口以及PHP设计模式(三):封装中,我们已经见过继承,也就是extends关键字。
和C/C++,Java,Python等语言一样,PHP也支持继承,而且和其他语言没有什么区别。
继承/Inheritance
还是用动物、鲸鱼和鲤鱼来举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
   |  <?php abstract class Animal {   protected $name;
    protected function chew($food) {     echo $this->name . " is chewing " . $food . ".\n";   }   protected function digest($food) {     echo $this->name . " is digesting " . $food . ".\n";   } }
  class Whale extends Animal {   public function __construct() {     $this->name = "Whale";   }   public function eat($food) {     $this->chew($food);     $this->digest($food);   } }
  class Carp extends Animal {   public function __construct() {     $this->name = "Carp";   }   public function eat($food) {     $this->chew($food);     $this->digest($food);   } }
  $whale = new Whale(); $whale->eat("fish"); $carp = new Carp(); $carp->eat("moss"); ?>
 
  | 
 
运行一下:
1 2 3 4 5 6
   |  $ php Inheritance.php Whale is chewing fish. Whale is digesting fish. Carp is chewing moss. Carp is digesting moss.
 
  | 
 
注意***$this***在Animal类、Whale类、Carp类中的用法。
上面的代码看似常见,实则暗含玄机。对于一个好的程序设计,需要:
类和类之间应该是低耦合的。
 
继承通常是继承自抽象类,而不是具体类。
 
通常直接继承抽象类的具体类只有一层,在抽象类中用protected来限定。
 
总结
合理的继承对于好的程序设计同样是必不可少的,结合abstract和protected,能让你编写出结构清晰的代码。