php 使用 __call實現(xiàn)重載功能示例
本文實例講述了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
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
- 解決PHP Opcache 緩存刷新、代碼重載出現(xiàn)無法更新代碼的問題
- PHP面向對象程序設計模擬一般面向對象語言中的方法重載(overload)示例
- PHP面相對象中的重載與重寫
- PHP中子類重載父類的方法【parent::方法名】
- PHP面向對象編程之深入理解方法重載與方法覆蓋(多態(tài))
- php函數(shù)重載的替代方法--偽重載詳解
- php繼承中方法重載(覆蓋)的應用場合
- PHP使用方法重載實現(xiàn)動態(tài)創(chuàng)建屬性的get和set方法
- PHP利用func_get_args和func_num_args函數(shù)實現(xiàn)函數(shù)重載實例
- php面向對象全攻略 (八)重載新的方法
- php面向對象的方法重載兩種版本比較
- PHP重載基礎知識回顧
相關文章
PHP識別二維碼的方法(php-zbarcode安裝與使用)
這篇文章主要介紹了PHP識別二維碼的方法,通過安裝ImageMagick和php-zbarcode擴展實現(xiàn)針對二維碼的識別功能,具有一定參考借鑒價值,需要的朋友可以參考下2016-07-07session在php5.3中的變化 session_is_registered() is deprecated in
在php 5.3中session_is_registered()已經是放棄使用了,大家在使用過程中需要注意一下了2013-11-11