php类虽然是单继承的,但是可以通过其它特殊的方式实现多重继承,比如使用接口实现,只要把类的特征抽象为接口,并通过实现接口的方式让对象有多重身份,通过这样就可以模拟多重继承了。
例子,用接口模拟多重继承。
<?php interface UserInterface{ //定义User的接口 function getname(); } interface TeacherInterface{ //teacher相关接口 function getLengthOfService(); } class User implements UserInterface { //实现UserInterface接口 private $name = "tom"; public function getName(){ return $this->name; } } class Teacher implements TeacherInterface { //实现TeacherInterface接口 private $lengthOfService = 5; // 工龄 public function getLengthOfService(){ return $this->lengthOfService; } } // 继承自User类,同时实现了TeacherInterface接口. class GraduateStudent extends User implements TeacherInterface { private $teacher ; public function __construct(){ $this->teacher = new Teacher(); } public function getLengthOfService(){ return $this->teacher->getLengthOfService(); } } class Act{ //注意这里的类型提示改成了接口类型 public static function getUserName(UserInterface $_user){ echo "Name is " . $_user->getName() ."<br>"; } //这里的类型提示改成了TeacherInterface类型. public static function getLengthOfService(TeacherInterface $_teacher){ echo "Age is " .$_teacher->getLengthOfService() ."<br>"; } } $graduateStudent = new GraduateStudent(); Act::getUserName($graduateStudent); Act::getLengthOfService($graduateStudent); //实现了有多重身份的一个对象. ?>
结果:
Name is tom
Age is 5