定义两个类,一个是person,一个是family;在family类中创建person类中的对象,把这个对象视为family类的一个属性,并调用它的方法处理问题,这种复用方式叫:“组合”。
类与类之间有一种父与子的关系,子类继承父类的属性和方法,称为继承。
在继承里,子类拥有父类的方法和属性,同时子类也可以有自己的方法和属性。
<?php class person{ public $name; public $gender; public function say(){ echo $this->name,"\tis",$this->gender,"\r\n"; } } class family{ public $people; public $location; public function __construct($p,$loc){ $this->people = $p; $this->location = $loc; } } $student = new person(); $student->name = 'Tom'; $student->gender = 'male'; $student->say(); $tom = new family($stduent,'peking'); print_r($tom); /* family object { [people] => person object { [name] => Tom [gender] => male } [location] => peking } */