2014-08-28 50 views
0

我想在Codeigniter模型中創建重載方法。我知道方法重載不支持像JAVA的PHP。所以我想知道哪一個是從以下兩個做到這一點最好的辦法還是請建議是否有任何其他正確的方式如何在codeigniter模型中使用方法開發

function mymethod($p1 = null, $p2 = null){ 
    if (isset($p1)){ 
     echo "My method has 1st parameter<br>"; 
    } 
    if (isset($p2)){ 
     echo "My method has 2nd parameter also"; 
    } 
    rest code.. 
} 

OR

public function __call($m , $p) 
{ 
    switch($m) 
    { 
     case "mymethod": 
      $count = count($p); 
      switch($count) 
      { 
       case "0": 
        return "You are passing 0 argument"; 
        break; 
       case "1": 
        return "You are passing 1 argument"; 
        break; 
       case "2": 
        return "You are passing 2 parameter"; 
        break; 
       case "3": 
        return "You are passing 3 parameter"; 
        break; 
       default: 
        throw new exception("Bad argument"); 
      } 
     default: 
      throw new exception("Function $m does not exists "); 
    } 
} 

回答

0

在方法重載

1)的這些方法的參數數量是不同的。 2.)參數類型不同(如 將浮點參數更改爲int)。上述

是用於方法超載條件

function mymethod($p1 = null, $p2 = null, $p3 = null){ 
    if (isset($p1)){ 
     echo "My method has 1st parameter<br>"; 
    } 
    if (isset($p2)){ 
     echo "My method has 2nd parameter also"; 
    } 
    rest code.. 
} 
相關問題