我有一個控制器函數3參數。默認參數是爲所有這些設置 當調用這個函數,如果我必須給第三個參數我怎麼能指定我傳遞第三個參數我怎麼能在codeigniter中指定給定的參數
public function manage_class($msg="",$id=0,$class_type="all")
i want to call this function only by the 3rd parameter
我有一個控制器函數3參數。默認參數是爲所有這些設置 當調用這個函數,如果我必須給第三個參數我怎麼能指定我傳遞第三個參數我怎麼能在codeigniter中指定給定的參數
public function manage_class($msg="",$id=0,$class_type="all")
i want to call this function only by the 3rd parameter
方法將首先設置可能更改默認值的參數,然後嚴格默認設置: public function manage_class($class_type="all", $msg="", $id=0)
。也許我錯了,或者什麼。
順便說一句,如果你只需要傳遞一個參數,爲什麼你傳遞其他兩個參數?
他所調用的功能是接受3個參數。他只是想給出最後一個參數,因爲無論如何他們都是默認的。 – Viridis
那麼在這種情況下,正如我前面,但接着說,一個數組,你可以定義函數manage_class:
manage_class($args = array())
{
//do something with this
echo $args['third'];
}
,然後你可以CAL manage_class像這樣的URL(網站名稱/ call_manage_class /空/空/富
call_manage_class($first, $second, $third)
{
if($first == null) {
$first = "";
}
//etc
manage_class(array('first'=>$third
'second'=>$second
'third'=>$third));
}
但是當我試圖傳遞null或空白字符串時,參數被ci甚至a跳過。沒有作爲一個論點 – Chinthu
的可能重複[我怎麼會跳過一個函數調用可選的參數?](http://stackoverflow.com/q/1066625/1612146) – George
可怕的方法是'manage_class(「」, 0,$ class_type)''。只需將不想使用的參數設置爲默認參數秒。這種方式無論如何都沒有改變。我知道這只是一個超級「修復」......但它會讓你獲得預期的結果。好的問題,我希望有人提出了一個更好的方法xD – Viridis
其實我必須從http:// sitename/controller/parameters – Chinthu