-1

I have a controller function with 3 argument.Default parameter is set for all of them When calling this function if i have to give the 3rd parameter only how can i specify i am passing the 3rd parameter

public function manage_class($msg="",$id=0,$class_type="all") 

i want to call this function only by the 3rd parameter 
Chinthu
  • 277
  • 3
  • 5
  • 16
  • possible duplicate of [How would I skip optional arguments in a function call?](http://stackoverflow.com/q/1066625/1612146) – George Dec 18 '13 at 09:32
  • terrible method would be `manage_class('', 0, $class_type)`. Just set the parameters you don't want to use to the default params. This way nothing gets changed anyway. I'm aware that this is just a superlazy 'fix' ... but it'll get you the expected result. Good question, i hope someone comes up with a better method xD – Viridis Dec 18 '13 at 09:32
  • actually i have to call this function from url like http://sitename/controller/parameters – Chinthu Dec 18 '13 at 09:36
  • In CI you can always just call the `sitename/controller` and add some `$_GET` parameters. Then you leave out all the parameters in your index() function, and handle your `$_GET` accordingly. – Viridis Dec 18 '13 at 09:41

3 Answers3

0

I think that if you pass null to the 1st and 2nd parameter, it will grab the default values

like:

manage_class(null,null,3rd_value)
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • actually i have to call this function from url like http://sitename/controller/parameters – Chinthu Dec 18 '13 at 09:38
  • If you call `NULL`, you're actually setting it as a parameter as far as i know. -- http://www.php.net/manual/en/functions.arguments.php – Viridis Dec 18 '13 at 09:39
0

Method would be to first set parameters with possible change of default value, and then strict defaults: public function manage_class($class_type="all", $msg="", $id=0). Maybe I've got you wrong, or something.

And by the way, if you need to pass only 1 parameter, why you passing other two as arguments?

Damaged Organic
  • 8,175
  • 6
  • 58
  • 84
  • The function he's calling is accepting 3 parameters. He just wants to give the last parameter since they're all default'd anyway. – Viridis Dec 18 '13 at 09:42
0

Well in that case you could define the function manage_class like i said above but then with an array:

manage_class($args = array())
{
//do something with this
echo $args['third'];
}

and then you can cal manage_class with an url like this (sitename/call_manage_class/null/null/foo

call_manage_class($first, $second, $third)
{
   if($first == null) {
   $first = "";
   }
   //etc
   manage_class(array('first'=>$third
                      'second'=>$second
                      'third'=>$third));
}
  • but when i tried to pass null or blank string the argument is skipped by ci even a . is not taking as an argument – Chinthu Dec 18 '13 at 10:03