Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Specify argument variable names when calling a function?

Tags:

php

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?

like image 875
Topher Hunt Avatar asked Aug 24 '26 20:08

Topher Hunt


2 Answers

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.

like image 160
abstr Avatar answered Aug 26 '26 09:08

abstr


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);
like image 45
alephreish Avatar answered Aug 26 '26 09:08

alephreish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!