Named Arguments in PHP

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 :

my_function(array(
    'my_param' => 10, 
    'other_param' => 'hello, world!', 
));

And, in your function, you’d read data from that unique array parameter :

function my_function(array $params) {

    // test if $params['my_param'] is set ; and use it if it is
    // test if $params['other_param'] is set ; and use it if it is
    // test if $params['yet_another_param'] is set ; and use it if it is
    // ...

}

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…

Method 2

You can get around that by having an array such as $array= array('arg1'=>'value1');
And then let the function accept the array such as function dostuff($stuff);
Then, you can check arguments using if(isset($stuff['arg1')){//do something.} inside the function itself

It’s just a work-around but maybe it could help

Method 3

You can fake C++-style optional arguments (i.e. all optional arguments are at the end) by checking for set variables:

function foo($a, $b)
{
  $x = isset($a) ? $a : 3;
  $y = isset($b) ? $b : 4;
  print("X = $x, Y = $yn");
}

@foo(8);
@foo();

It’ll trigger a warning, which I’m suppressing with @. Not the most elegant solution, but syntactically close to what you wanted.


Edit. That was a silly idea. Use variadic arguments instead:

// faking foo($x = 3, $y = 3)
function foo()
{
  $args = func_get_args();
  $x = isset($args[0]) ? $args[0] : 3;
  $y = isset($args[1]) ? $args[1] : 3;
  print("X = $x, Y = $yn");
}

foo(12,14);
foo(8);
foo();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x