php 多繼承的幾種常見實現(xiàn)方法示例
本文實例講述了php 多繼承的幾種常見實現(xiàn)方法。分享給大家供大家參考,具體如下:
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);
}
}
// 恢復(fù)默認的行為,會引發(fā)一個方法不存在的致命錯誤
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();
運行結(jié)果:
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 "接口繼承,要實現(xiàn)所有相關(guān)抽象方法!";
echo "<br>";
}
function dancing($name){
echo $name."正在跳舞!";
}
}
$demo=new testC();
$demo->echostr();
$demo->dancing("模特");
運行結(jié)果:
接口繼承,要實現(xiàn)所有相關(guān)抽象方法!
模特正在跳舞!
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP實現(xiàn)單例模式建立數(shù)據(jù)庫連接的方法分析
這篇文章主要介紹了PHP實現(xiàn)單例模式建立數(shù)據(jù)庫連接的方法,結(jié)合實例形式分析了PHP單例模式的概念、原理及使用單例模式實現(xiàn)數(shù)據(jù)庫連接的相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
PHP實現(xiàn)的常規(guī)正則驗證helper公共類完整實例
這篇文章主要介紹了PHP實現(xiàn)的常規(guī)正則驗證helper公共類,結(jié)合完整實例形式分析了php針對常規(guī)的電話、手機、郵箱、賬號等進行正則驗證的操作技巧,需要的朋友可以參考下2017-04-04
php5.4以上版本GBK編碼下htmlspecialchars輸出為空問題解決方法匯總
這篇文章主要介紹了php5.4以上版本GBK編碼下htmlspecialchars輸出為空問題解決方法匯總,本文給出多種解決這個問題的方法,需要的朋友可以參考下2015-04-04
thinkphp操作mongo數(shù)據(jù)的三種方法
這篇文章主要給大家介紹了thinkphp操作mongo數(shù)據(jù)的三種方法,使用tp中的擴展,使用tp中的db類和使用MongoDB PHP驅(qū)動程序這三種方法,并通過代碼講解的非常詳細,需要的朋友可以參考下2023-12-12

