我想在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..
}
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 :(得分: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..
}