>  기사  >  백엔드 개발  >  PHP의 다중 상속 방법

PHP의 다중 상속 방법

不言
不言원래의
2018-05-03 11:38:583015검색

이 글은 주로 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();
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의 다중 상속 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.