【发布时间】: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