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ù)默認(rèn)的行為,會引發(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();
運(yùn)行結(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("模特");
運(yùn)行結(jié)果:
接口繼承,要實現(xiàn)所有相關(guān)抽象方法!
模特正在跳舞!
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php中ftp_chdir與ftp_cdup函數(shù)用法
這篇文章主要介紹了php中ftp_chdir與ftp_cdup函數(shù)用法,以實例形式講述了PHP中的FTP目錄操作技巧,具有一定的借鑒價值,需要的朋友可以參考下2014-11-11
php出現(xiàn)Cannot modify header information問題的解決方法大全
我做了一個統(tǒng)一的出錯提示函數(shù),在函數(shù)執(zhí)行里面,先處理出錯的地址寫入cookie以方便用戶登陸以后可以直接跳轉(zhuǎn)到要執(zhí)行的這個頁面,可是發(fā)現(xiàn)在服務(wù)器上測試時,竟然提示本地沒有出現(xiàn)的錯誤: Warning: Cannot modify header information - headers already sent by....2008-04-04
PHP中shuffle數(shù)組值隨便排序函數(shù)用法
這篇文章主要介紹了PHP中shuffle數(shù)組值隨便排序函數(shù)用法,可以比較簡單的實現(xiàn)對數(shù)組的隨機(jī)排序,需要的朋友可以參考下2014-11-11
淺析Dos下運(yùn)行php.exe,出現(xiàn)沒有找到php_mbstring.dll 錯誤的解決方法
本篇文章是對在Dos下運(yùn)行php.exe,出現(xiàn)沒有找到php_mbstring.dll 錯誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
淺談并發(fā)處理PHP進(jìn)程間通信之外部介質(zhì)
進(jìn)程間通信(IPC,Inter-Process Communication),多進(jìn)程開發(fā)中,進(jìn)程間通信是一個永遠(yuǎn)也繞不開的問題。在 web開發(fā)中,我們經(jīng)常遇到的并發(fā)請求問題,本質(zhì)上也可以作為進(jìn)程間通信來處理。2021-05-05

