為了提高代碼的復用性,降低代碼的耦合(組合實現的兩種方式)
模式一:
1 <?php
2 //組合模式一
3 class Person{
4 public function eat(){
5 echo "eat.<br/>";
6 }
7 }
8
9 class Phone{
10 public function call(){
11 echo "phone call.<br/>";
12 }
13 }
14
15 //學生也需要call()這個方法,為了提高代碼的復用性(組合)
16 class Student extends Person{
17 private $people;
18 public function learning(){
19 echo "learn.<br/>";
20 }
21 public function func($class, $method){//兼容多個類的多個方法
22 $this->people = new $class;
23 $this->people->$method();
24 }
25 }
26
27 $student = new Student();
28 $student->eat();
29 $student->func('Phone', 'call');
30 $student->learning();
模式二:
