欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php 使用 __call實(shí)現(xiàn)重載功能示例

 更新時(shí)間:2019年11月18日 10:00:32   作者:ztblog  
這篇文章主要介紹了php 使用 __call實(shí)現(xiàn)重載功能,結(jié)合實(shí)例形式分析了PHP使用__call實(shí)現(xiàn)重載的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了php 使用 __call實(shí)現(xiàn)重載功能。分享給大家供大家參考,具體如下:

<?php
/**
 * Created by PhpStorm.
 * User: funco
 * Date: 17-6-9
 * Time: 下午1:39
 */
class MulStat
{
  // showClass 可以接受0個(gè)參數(shù)
  private function showClass() {
    echo "this is class ".__CLASS__;
  }

  // showString 可以接受一個(gè)參數(shù)
  private function showString($str) {
    echo "string is ".$str;
  }

  // __call方法 可以獲取實(shí)例化對(duì)象調(diào)用的成員函數(shù)名和向該被調(diào)函數(shù)傳遞的參數(shù)個(gè)數(shù)
  public function __call($name, $args) {
    // 先判斷要調(diào)用的函數(shù)名$name
    if($name == "showInfo"){
      // 然后可以根據(jù)參數(shù)($args)數(shù)量判斷調(diào)用哪個(gè)成員函數(shù)
      switch(count($args)) {           // count可以計(jì)算數(shù)組元素個(gè)數(shù)
        case 0:
          $this->showClass();break;
        case 1:
          $this->showString($args[0]);break;
      }// switch
    }// if
  }
}

//實(shí)例化MulStat類
$mulStat = new MulStat();

echo "\$mulStat->showInfo(\"funco 小風(fēng)\"):\n";
$mulStat->showInfo("funco 小風(fēng)");

// 兩次換行 便于觀察結(jié)果
echo "\n\n";

echo "\$mulStat->showInfo():\n";
$mulStat->showInfo();

運(yùn)行結(jié)果:

$mulStat->showInfo("funco 小風(fēng)"):
string is funco 小風(fēng)

$mulStat->showInfo():
this is class MulStat

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論