本文實例講述了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面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- PHP Trait代碼復用類與多繼承實現方法詳解
- PHP中的Trait 特性及作用
- PHP中trait使用方法詳細介紹
- PHP中Trait及其應用詳解
- 淺談PHP中的Trait使用方法
- PHP中的traits簡單使用實例
- PHP中的traits實現代碼復用使用實例
- PHP的Trait機制原理與用法分析
- PHP接口多繼承及tarits實現多繼承效果的方法
- PHP 使用 Trait 解決 PHP 單繼承問題詳解