php怎样实现多继承,有哪些方法?
Admin 2021-05-25 群英技术资讯 931 次浏览
PHP如何实现多继承?PHP本身是不支持多继承的,只支持单继承,但是我们能通过的别的方法来实现php多继承。下面就给大家分享一些实现php多继承的常见方法。
本文实例讲述了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 "<br>"; } function dancing($name){ echo $name."正在跳舞!"; } } $demo=new testC(); $demo->echostr(); $demo->dancing("模特");
运行结果:
接口继承,要实现所有相关抽象方法!
模特正在跳舞!
关于php多继承的实现就介绍到这,上述示例有一定的借鉴价值,有需要的朋友可以参考学习,希望对大家学习有一定的帮助,想要了解更多php多继承内容大家可以继续关注其他文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
什么是依赖注入?本篇文章带大家了解一下PHP中的依赖注入,介绍一下依赖注入出现的原因、依赖注入的应用,希望对大家有所帮助!
1.imagecreate()函数imagecreate()函数是基于一个调色板的画布。<?php$im=imagecreate(200,80);//创建一个宽200,高80的画布。$white=imagecolorallocate($im,225,35,180);//设置画布的背景颜色imagegif(
Swoole在1.8.6版本提供了全新的异步MySQL客户端,底层自行实现了MySQL的通信协议,无需依赖其他第三方库,直接使用swoole_mysql->__construct命令,创建异步mysql客户端。
本篇文章小编给大家分享一下python实现修改xml文件内容代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
很多朋友都有遇到PHP中,使用fopen来生成文件时,如果文件名是中文,那么就会出现乱码的情况,那么我们应该如何解决这个问题呢?下面给大家介绍一个解决方法。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008