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.
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:
In the route, the parameters are named
first_name
,last_name
, andcolor
.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:
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:
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.