2014-08-28 49 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.. 
} 
相关问题