php 多继承的几种常见实现方法示例

作者:ztblog 时间:2023-11-22 19:03:19 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/ztblog/article/details/80171393

标签:php,多继承
0
投稿

猜你喜欢

  • python函数参数(必须参数、可变参数、关键字参数)

    2023-09-17 07:54:28
  • IE9报“DOM Exception: INVALID_CHARACTER_ERR (5)”错误的原因及解决办法

    2011-09-01 19:11:07
  • asp三天学好ADO对象之第二天

    2008-10-09 12:49:00
  • MS SQL2000 数据库自动备份方法

    2010-07-22 19:52:00
  • [整理版]防止Access数据库被下载的9种方法

    2007-08-10 09:31:00
  • MySQL应用技巧之存取权限

    2010-08-08 09:10:00
  • 详解MySQL数据库之更新语句

    2010-08-08 09:15:00
  • SQL 语句中的通配符

    2007-10-11 18:03:00
  • Tab(选项卡)的产品设计原则及应用[译]

    2009-07-09 19:05:00
  • Dreamweaver打造多彩文字链接

    2007-11-11 18:59:00
  • 如何判断js脚本加载完成

    2008-11-04 13:53:00
  • W3C网页内容无障碍指南2.0(WCAG)

    2008-11-20 13:40:00
  • SQL参数化查询的另一个理由 命中执行计划

    2012-08-21 10:31:16
  • PHP中的函数嵌套层数限制分析

    2023-11-21 08:43:24
  • js倒记时代码,自定义时间改变时间,日期到时事件,日期格式。

    2010-08-08 08:51:00
  • 比较SQL Server与Oracle、DB2三种数据库

    2008-09-12 17:24:00
  • 38个Asp内置函数介绍

    2008-11-27 16:25:00
  • SQL Server中的执行引擎入门 图解

    2012-06-06 20:08:26
  • border:none;与border:0;的区别

    2009-11-27 19:04:00
  • PLSQL导入dmp文件的详细完整步骤

    2023-06-26 11:45:32
  • asp之家 网络编程 m.aspxhome.com