In C#, there is a new feature coming with 4.0 called Named Arguments and get along well with Optional Parameters.
private static void writeSomething(int a = 1, int b = 2){
// do something here;
}
static void Main()
{
writeSomething(b:3); // works pretty well
}
I was using this option to get some settings value from users.
In PHP, I cannot find anything similar except for the optional parameters but I am accepting doing $.fn.extend (jQuery) kind of function :
function settings($options)
{
$defaults = array("name"=>"something","lastname"=>"else");
$settings = array_merge($defaults,$options);
}
settigs(array("lastname"=>"John");
I am wondering what kind of solutions you are using or you would use for the same situation.
As you found out, named arguments don’t exist in PHP.
But one possible solution would be to use one array as unique parameter — as array items can be named :
And, in your function, you’d read data from that unique array parameter :
Still, there is one major inconvenient with this idea : looking at your function’s definition, people will have no idea what parameters it expects / they can pass.
They will have to go read the documentation each time they want to call your function — which is not something one loves to do, is it ?
Additionnal note : IDEs won’t be able to provide hints either ; and phpdoc will be broken too…