3

This might seem like an academic or useless topic, but I'm curious.

When developing web pages with PHP, I often need to call functions that take several arguments. I frequently need to look up the spec for the function (on php.net or in my include files, if it's a function I defined) to remind myself what the variables are and what order they're in and what the defaults are, etc. I imagine many of you can relate to this.

A function defined like this:

function do_something_awesome ($people_array, $places_recordset, $num_cycles, $num_frogs,
  $url = '?default=yes', $submit_name = 'default_submit_label') {
  ...
}

when called, might look like this:

$result = do_something_awesome($names, $rsTowns, $c, $f);

My question is this: I'd like to write my code in a way that reminds me of which argument corresponds to each variable, during function calls like this. Is it ever legal to call a function as follows?

$result = do_something_awesome($people_array = $names, $places_recordset = $rsTowns, 
  $num_cycles = $c, $num_frogs = $f);

If not in PHP, are there other languages where method calls can be made in this way?

1
  • 2
    Search for "named arguments" or "named parameters". You can call functions like that in PHP, but you're going to have obvious side-effects (introducing variables named $people_array into the current scope and destroying any value they might have already had). Commented Oct 3, 2013 at 18:07

4 Answers 4

1

To answer your first question:

My question is this: I'd like to write my code in a way that reminds me of which argument corresponds to each variable, during function calls like this.

AFAIK, many PHP coders do it by passing in an associative array as the only argument. However, you'll have to do your own variables checking inside the called function.

$result = do_something_awesome(array(
    'people_array' => $names,
    'places_recordset' => $rsTowns, 
    'num_cycles' => $c, 
    'num_frogs' => $f
));

As for:

Is it ever legal to call a function as follows?

It won't cause any PHP errors, but what you are effectively doing is:

$result = do_something_awesome( expression, expression, expression, expression );

See: PHP Functions arguments

PHP won't know to put $people_array = ... or $num_frogs = ... in their corresponding places when you decide to switch their order around. Furthermore, as DCoder said, these expressions actually take place in the current scope, and will change any pre-existing variables without letting you know.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @abstr. I've used associative arrays some, and it looks like they'll work well enough for functions that I've defined.
1

What about using an object as the only argument:

function my_function($arguments) {
   if (!is_object($arguments)) throw new Exception();
   $default_values = array('arg1' => 'value1', 'arg2' => 'value2');
   foreach ($default_values as $key => $default_value)
      if (!isset($arguments->$key)) $arguments->$key = $default_value;
   ## do the job ##
}

## and then
$my_arguments = new stdClass();
$my_arguments->arg2 = 'some_value';
my_function($my_arguments);

Comments

0

You can try this out:

$bas = 'This is passed to the function.';
$bar = 'This will be modified.';

function foo($bar)
{
    echo $bar;
}

foo($bar = $bas);
echo $bar;

The output from this script would be 'This is passed to the function.This is passed to the function.'. So like DCoder said, while you can use them and it's perfectly legal but if you had other variables with the same name as the function arguments, this will overwrite them (in this case the original $bar was overwritten).

Comments

0

As of PHP 8 you can use named arguments. Here's the example from php.net:

As of PHP 8.0.0, named arguments can be used to skip over multiple optional parameters.

Example #9 Correct usage of default function arguments

<?php
function makeyogurt($container = "bowl", $flavour = "raspberry", $style = "Greek")
{
    return "Making a $container of $flavour $style yogurt.\n";
}

echo makeyogurt(style: "natural");
?>

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.