本文主要内容:
php接口实现多重继承。
说明:
PHP类是单继承的,不过可以通过其它特殊的方式实现多重继承,比如使用接口实现,只要把类的特征抽象为接口,并通过实现接口的方式让对象有多重身份,如此便可以模拟多重继承。
这里分享一个用接口模拟多重继承的例子:
复制代码 代码示例:
<?
/**
* php接口实现多重继承
* edit: www.jb200.com
*/
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