4-8-1.php 组合 class User{ private $name = "tom"; public function getName(){ return $this->name; } } class Teacher{ private $lengthOfService = 5; // 工龄 public function getLengthOfService(){ return $this->lengthOfService; } } // 上面的类中的set方法就不写了. // 如果有个研究生,既是学生也算工龄. class GraduateStudent extends User { private $teacher ; public function __construct(){ $this->teacher = new Teacher(); } public function getLengthOfService(){ return $this->teacher->getLengthOfService(); } } $graduateStudent = new GraduateStudent(); echo "name is ".$graduateStudent->getName().""; echo "lengthOfService is ".$graduateStudent->getLengthOfService(); ?>