1

I am trying to create a little MVC framework for myself and am using a url structure similar to: http://www.example.com/className/methodName/var1/var2/var3. I want to make it able to accomodate for any number of variables.

I have all of the information how I need it, except for the /var1/var2/var3 part. Currently I have it in an array exploded() upon the "/", so: var[0] = "var1",var[1] = "var2",var[2] = "var3"

Since the method which will be called will be unknown and each method can require a different amount parameters I want to figure out a way to be able to pass the parameters as:

$controller->$function($var1, $var2, $var3);

rather than

$controller->$function($var);

I realize I can string together a comma delimited variable such as $var1, $var2, $var3 and use eval() to do what I want, but I am wondering if there is a better way to go about it. I would rather not use eval() for this as it will be using user submitted data.

Worst case scenario, I figure I would just try to cleanse the data before the eval, but I'd still like to avoid it.

One other potential Idea I had was to use a foreach() and loop through each element inside of the the method call. But even that seems a little messy to me.

On a side note, is the standard way to pass a variable amount of parameters to group them together in an array and pass in a single parameter?

If anyone knows of a more elegant way to do this, I would really appreciate it.

2 Answers 2

2

Use call_user_func_array

call_user_func_array(array($controller, $function), array($var1, $var2...));
Sign up to request clarification or add additional context in comments.

4 Comments

Agreed. Also worth looking at overloading / magic methods in PHP. Specifically: php.net/manual/en/language.oop5.overloading.php#object.call
have you actually read the manual page to which you linked to ?
I've fixed the arguments, (I think it was a typo).
@tereško, I'm sorry, but what did I miss? (Except the thing Wouter fixed.)
1

@Dogbert's answer would complement what I'm about to suggest.

You could create a function that accepts no parameters and you're free to pass as many parameters (or no parameters at all), you'll only have to use PHP functions like: func_get_args(), func_num_args() and func_get_arg(int arg_index) to do your manipulation.

An Example follows below:

function syncOrderRelation(){
    $num_arguments = func_num_args();
    $arguments = func_get_args();
    #gets all the arguments in the request
    list($userID,$spID,$productID) = $arguments;
    #if you know what you're expecting or
    for($i = 0; $i < $num_arguments; $i++){
            $argument_at_index = func_get_arg($i);
    }
}    

You could call this function in the following ways:

syncOrderRelation();
syncOrderRelation($var1,$var2,var3);
syncOrderRelation($var1);
#or you could call it as @Dogbert suggested since the function will accept whatever
#arguments you supply    

The rest is up to you...Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.