2014-08-28 2 views
0

Codeigniter의 모델에서 오버로드 메서드를 만들려고합니다. 메서드 오버로드가 JAVA 같은 PHP에서 지원되지 않는다는 것을 알고 있습니다. 그래서 내가 다른 올바른 방법이 방법은codeigniter 모델에서 메서드 oveloading을 사용하는 방법

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.. 
} 

또는

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.) 매개 변수 유형이 다릅니다 (예 : 은 float 인 매개 변수를 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.. 
}