【问题标题】:How to use method oveloading in codeigniter models如何在 codeigniter 模型中使用方法重载
【发布时间】:2014-08-28 05:32:55
【问题描述】:

我想在 Codeigniter 的模型中创建重载方法。我知道像 JAVA 这样的 php 不支持方法重载。所以我想从以下两个中知道哪一个是最好的方法,或者请建议是否有其他正确的方法

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 ");
    }
}

【问题讨论】:

    标签: php codeigniter oop model overloading


    【解决方案1】:

    在方法重载中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多