PHP 接口实现多重继承的实例分享
本文介绍下,在 php 编程中通过接口实现多重继承的方法,通过实例来学习 php 继承的应
用,有需要的朋友参考下吧。
本文主要内容:
php 接口实现多重继承。
说明:
PHP 类是单继承的,不过可以通过其它特殊的方式实现多重继承,比如使用接口实现,只
要把类的特征抽象为接口,并通过实现接口的方式让对象有多重身份,如此便可以模拟多
重继承。
这里分享一个用接口模拟多重继承的例子:
代码示例
:
<?
/**
* php 接口实现多重继承
* edit: www.jbxue.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; // 工 龄