php多继承吗,php中实现多继承的方式有哪些

php中实现多继承的方式有哪些

发布时间:2021-02-17 18:28:14

来源:亿速云

阅读:56

作者:Leah

这篇文章给大家介绍php中实现多继承的方式有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

具体如下:class Parent1 {

function method1() {}

function method2() {}

}

class Parent2 {

function method3() {}

function method4() {}

}

class Child {

protected $_parents = array();

public function Child(array $parents=array()) {

$this->_parents = $parents;

}

public function __call($method, $args) {

// 从“父类"中查找方法

foreach ($this->_parents as $p) {

if (is_callable(array($p, $method))) {

return call_user_func_array(array($p, $method), $args);

}

}

// 恢复默认的行为,会引发一个方法不存在的致命错误

return call_user_func_array(array($this, $method), $args);

}

}

$obj = new Child(array(new Parent1(), new Parent2()));

print_r( array($obj) );die;

$obj->method1();

$obj->method3();

运行结果:Array

(

[0] => Child Object

(

[_parents:protected] => Array

(

[0] => Parent1 Object

(

)

[1] => Parent2 Object

(

)

)

)

)interface testA{

function echostr();

}

interface testB extends testA{

function dancing($name);

}

class testC implements testB{

function echostr(){

echo "接口继承,要实现所有相关抽象方法!";

echo "
";

}

function dancing($name){

echo $name."正在跳舞!";

}

}

$demo=new testC();

$demo->echostr();

$demo->dancing("模特");

运行结果:接口继承,要实现所有相关抽象方法!

模特正在跳舞!

关于php中实现多继承的方式有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值