0514-86177077
9:00-17:00(工作日)
本文實例講述了php 使用 __call實現(xiàn)重載功能。分享給大家供大家參考,具體如下:
?php /** * Created by PhpStorm. * User: funco * Date: 17-6-9 * Time: 下午1:39 */ class MulStat { // showClass 可以接受0個參數(shù) private function showClass() { echo "this is class ".__CLASS__; } // showString 可以接受一個參數(shù) private function showString($str) { echo "string is ".$str; } // __call方法 可以獲取實例化對象調用的成員函數(shù)名和向該被調函數(shù)傳遞的參數(shù)個數(shù) public function __call($name, $args) { // 先判斷要調用的函數(shù)名$name if($name == "showInfo"){ // 然后可以根據(jù)參數(shù)($args)數(shù)量判斷調用哪個成員函數(shù) switch(count($args)) { // count可以計算數(shù)組元素個數(shù) case 0: $this->showClass();break; case 1: $this->showString($args[0]);break; }// switch }// if } } //實例化MulStat類 $mulStat = new MulStat(); echo "\$mulStat->showInfo(\"funco 小風\"):\n"; $mulStat->showInfo("funco 小風"); // 兩次換行 便于觀察結果 echo "\n\n"; echo "\$mulStat->showInfo():\n"; $mulStat->showInfo();
運行結果:
$mulStat->showInfo("funco 小風"): string is funco 小風 $mulStat->showInfo(): this is class MulStat
$mulStat->showInfo("funco 小風"): string is funco 小風
$mulStat->showInfo(): this is class MulStat
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
標簽:平涼 內江 深圳 江門 駐馬店 石嘴山 大同 雙鴨山
上一篇:PHP中通過getopt解析GNU C風格命令行選項
下一篇:php傳值和傳引用的區(qū)別點總結
Copyright ? 1999-2012 誠信 合法 規(guī)范的巨人網絡通訊始建于2005年
蘇ICP備15040257號-8