Welcome Guest, Not a member yet? Register   Sign In
nasty code - varaiable variables varaible paramaters
#1

[eluser]frenzal[/eluser]
this is more of a general php issue rather than CI i guess. What i'm working on is a controller that depending on the page loads certain modules (libraries) and accesses a specific method (in the db as a content part) these content parts get stuck together forming some sort of page. Anyway I found on how to do the whole varaible function thing but I need a nice way to deal with my parameters. Right now i have this

Code:
switch(count($p)){
                            case 1: $function = $this->{$con["module"]}->{$con["function"]}($p[0]); break;
                            case 2: $function = $this->{$con["module"]}->{$con["function"]}($p[0], $p[1]); break;                        
                            case 3: $function = $this->{$con["module"]}->{$con["function"]}($p[0], $p[1], $p[2]); break;                            
                            case 4: $function = $this->{$con["module"]}->{$con["function"]}($p[0], $p[1], $p[2], $p[3]); break;                                                        
                            case 5: $function = $this->{$con["module"]}->{$con["function"]}($p[0], $p[1], $p[2], $p[3], $p[4]);    break;                        
                            case 6: $function = $this->{$con["module"]}->{$con["function"]}($p[0], $p[1], $p[2], $p[3], $p[4], $p[5]); break;
                            default: $function = $this->{$con["module"]}->{$con["function"]}(); break;                                                                        
                        }

Sure it works, but it feels so wrong. The array of parameters ($p) could contain any type or number of parameters depending on the funciton being called, all the parameter values are from the db too. Any suggestions, or generally a better way to deal with this thing?

Cheers
#2

[eluser]xwero[/eluser]
Why don't you let the method you call take care of the array?
Code:
if(count($p) == 0)
{
   $function = $this->{$con["module"]}->{$con["function"]}();
}
else
{
   $function = $this->{$con["module"]}->{$con["function"]}($p);
}
#3

[eluser]frenzal[/eluser]
possible, but I also access the libraries directly and prefer not _having_ to pass an array, it would add a bit of overhead to each function and seem a bit "messy" too
#4

[eluser]xwero[/eluser]
Where is the overhead?
Code:
function one($one,$two,$three)
{
   return $one.' '.$two.' '.$three;
}
// vs
function one($array)
{
   return $array[0].' '.$array[1].' '.$array[2];
}
The only overhead i can think of is doing following
Code:
function one($array)
{
    list($one,$two,$three) = $array;
    return $one.' '.$two.' '.$three;
}
So it's only one line to create meaningful variables.

There is no func_set_args or something similar i know of. If you are going to support n parameters i think you will have no other option?
#5

[eluser]frenzal[/eluser]
thanks xwero, i'll consider it, if i pass an array i would use the list function because $array[0], $array[1] etc.. is a bit vague in code.
#6

[eluser]xwero[/eluser]
The list function is pretty fast as an alternative you could use extract but then you are not able to assign the variable name.

If you can think of a better solution let us know, i've been in situations where i needed to set function parameters too. But i could work around it.
#7

[eluser]Seppo[/eluser]
Why dont you just...
Code:
call_user_func_array(array(&$this->{$con["module"]}, $con["function"]), $p);
#8

[eluser]frenzal[/eluser]
aha, now that's a nice looking solution Smile
#9

[eluser]xwero[/eluser]
I forgot all about that one, that is a very nice solution




Theme © iAndrew 2016 - Forum software by © MyBB