Welcome Guest, Not a member yet? Register   Sign In
one controller method with multiple input params
#1

[eluser]ronnixw[/eluser]
example from user guide:
class Products extends Controller {

function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}

my problem:
I want to call method shoes with dynamic param count ( 2 , 3 or more ) not only with stacic defined.
is it possible?

best regards,
ronnixw
#2

[eluser]Rodrigo Ferreira[/eluser]
Take a look at PHP func_get_args()
#3

[eluser]ronnixw[/eluser]
but if I call function with non maching param count - there is an error?!
#4

[eluser]Clifford James[/eluser]
Code:
class Products extends Controller {
  function shoes()
  {
    $params = func_get_args();
  }
}
#5

[eluser]Unknown[/eluser]
[quote author="Clifford James" date="1292359229"]
Code:
class Products extends Controller {
  function shoes()
  {
    $params = func_get_args();
  }
}
[/quote]

is this considered good practice to use on a regular basis, or should it only be used in special/specific situations? I usually just define the $params with a default value which controls the behavior if there is no input.
Code:
function shoes($param1 = 'default-1', $param2 = 'default-2')
  {
    if($param1 === 'default-1' && $param2 === 'default-2')
    {
        //define something
    }
    else
    {
        //pass the value of $param1 and $param2 to some function
    }
    $this->load->view('theview', $data);
  }

I use this a lot for things such as table of contents so you don't have to define an exception for 'toc' (its the default behavior) e.g., controller/method/ displays the same as controller/method/toc.

would I be better off redesigning it to around func_get_args(); ?
#6

[eluser]Unknown[/eluser]
[quote author="ronnixw" date="1292358755"]but if I call function with non maching param count - there is an error?![/quote]

Not necessarily. You can handle the exception using a default procedure.

Code:
function shoes()
{
    switch(func_num_args()){
        case 1:
            // do something with 1 param
            echo "called with 1 param.";
            break;
        case 2:  // do something with 2 param
            echo "called with 2 params.";
            break;
        default:
            echo "Invalid Params";
            // throw new Exception("Invalid Params");
            /* Uncomment the line above if you want to throw an error. */
    }
}
#7

[eluser]Atharva[/eluser]
Another simple way is to use array as parameters so that you can pass as many parameters as you like. So there is no need to give default/static values. For sending emails, I use this method
Code:
$param['from'] = '[email protected]';
$param['to'] = '[email protected]';
$param['subject'] = 'subject';
.
.
$this->some_model->send_mail($param);

function send_mail($param){
$from = $param['from'];
$to = $param['to'];
$subject = $param['subject'];
.
.
.
code to send mail
}


You can utilize this technique according to your needs.
#8

[eluser]ronnixw[/eluser]
big thanks for reply and examples Smile)




Theme © iAndrew 2016 - Forum software by © MyBB