php类的多继承


1、PHP没有多继承的特性。即使是一门支持多继承的编程语言,我们也很少会使用这个特性。在大多数人看来,多继承不是一种好的设计方法。想要给某个类添加额外的特性,不一定要使用继承。这里我提供一种模拟多继承的方法以供参考。

 

PHP有一个魔术方法,叫做__call。当你调用一个不存在的方法时,这个方法会被自动调用。这时,我们就有机会将调用重定向到一个存在的方法。继承多个父类的子类,寻找方法的过程一般是这样的:

本身的方法 -> 父类1的方法 -> 父类2的方法...

 

模拟过程大致是这样:将各个父类实例化,然后作为子类的属性。这些父类提供一些公有的方法。当子类拥有某方法时,__call()函数不会被调用。这相当于“覆盖”了父类的方法。当调用了不存在的方法时,通过__call()方法依次从父类中寻找可以调用的方法。虽然这不是完备的多继承,但可以帮助我们解决问题。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
class  Parent1 {
     function  method1() {}
     function  method2() {}
}
class  Parent2 {
     function  method3() {}
     function  method4() {}
}
class  Child {
     protected  $_parents  array ();
     public  function  Child( array  $parents = array ()) {
         $_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()));
$obj ->method1();
$obj ->method3();

 

这里没有涉及属性的继承,但实现起来并不困难。可以通过__set()和__get()魔术方法来模拟属性的继承。请你动手实践。

2、php中的类只能继承一个父类,如果要继承多个类应采用接口

下面来看一个接口多继承的示例,代码如下:

  1. <?php 
  2. interface father{ 
  3. function shuchu(); 
  4. interface mother{ 
  5. function dayin($my); 
  6. interface fam extends father,mother{ 
  7. function cook($name); 
  8. class test implements fam{ 
  9. function dayin($my){ 
  10. echo "我的名字是:".$my;  
  11. echo "<br>"; 
  12. function shuchu(){ 
  13. echo "接口继承,要实现两个抽象方法"; 
  14. echo "<br>"; 
  15. function cook($name){ 
  16. echo "平时经常做饭的人是:".$name;  
  17. $t=new test(); 
  18. $t->shuchu(); 
  19. $t->dayin("小强");  
  20. $t->cook("妈妈");  
  21. ?> 


示例运行结果:

 

接口继承,要实现两个抽象方法
我的名字是:小强
平时经常做饭的人是:妈妈
 

 


这段代码由于接口继承了两个接口,所有实例时要把这三个抽象类的所有抽象方法都实例,总共有三个。看完这两个例子,你应该对接口的继承熟悉了吧,其实就一个单继承和多继承,只要实现了所有相关的抽象方法就可以了。

3、php5.4以上可以用trait

<?php
trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
}

class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
}
?>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM