Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PHP function or method arguments in any order?

Lets say I define class with method like this:

class Test {
    public function doStuff($a, $b, $c) {
    // -- Do stuff --
    }
}

Is it possible to use this method but with arguments in different order, like this:

$test = new Test();
$test->doStuff($b, $c, $a);

They would have the same names, but just different order.

I see Symfony2 can do it with its dispatcher, you can use arguments in any order you want. Link: Symfony2 controller can do it

The question is, how to make this work? How can Symfony2 invoke appropriate action controller, that can then accept arguments in any order you like?

Edit: I cant use arrays, and I do know that php does not use named arguments. But somehow Symfony2 manage to do it.

like image 515
otporan Avatar asked Oct 19 '12 20:10

otporan


People also ask

When calling a function the arguments can be in any order?

Let's take a function to divide two numbers, and return the quotient. We can call this function with arguments in any order, as long as we specify which value goes into what. As you can see, both give us the same thing. These are keyword python function arguments.

Does the order of arguments in a function matter?

Yes, it matters. The arguments must be given in the order the function expects them. C passes arguments by value. It has no way of associating a value with an argument other than by position.

Which PHP function is used to build a function that accepts any number of arguments?

Which of the following PHP functions accepts any number of parameters? Explanation: func_get_args() returns an array of arguments provided. One can use func_get_args() inside the function to parse any number of passed parameters.

What is default argument in PHP precedence?

The default arguments must be constant expressions. They cannot be variables or function calls. PHP allows you to use a scalar value, an array, and null as the default arguments.


1 Answers

I think you are misunderstanding what Symfony is saying. You can't pass the arguments to the controller action in any order, it has to be in a specific order. What they are doing that is dynamic, however, is figuring out what order your routing parameters are in inside the function definition.

For example we define a route:

pattern:      /hello/{first_name}/{last_name}
defaults:     { _controller: AcmeHelloBundle:Hello:index, color: green }

In the route, the parameters are named first_name, last_name, and color.

What they are saying, is that it doesn't matter what order you use for the parameters in the action.

Each of the following are equivalent:

public function indexAction($first_name, $last_name, $color) {...}
public function indexAction($color, $last_name, $first_name) {...}
public function indexAction($last_name, $color, $first_name) {...}

Since your arguments are named the same as the parameters in the route, Symfony figures out what the correct order of the arguments is based on your definition.

If you were to call the following action manually:

public function indexAction($first_name, $last_name, $color)

Then the arguments still must be passed in as $first_name, $last_name, $color and not in any other order. Using a different order would just associate the wrong values with the arguments. Symfony just doesn't care what order you define your function in since it determines the order because your routing parameters must be named the same thing as your method arguments.

Hopefully that clears up the confusion.

like image 135
drew010 Avatar answered Sep 30 '22 17:09

drew010