3

What's the best way to structure a function, that has parameters that can be null?

Eg

    function newDate($day, $time = null, $overRide) {

        Do something with the variables

    }

# newDate('12', '', 'yes');

Would it be to simply restructure the function as follows:

    function newDate($day, $overRide, $time = null) {

        Do something with the variables

    }

# newDate('12', 'yes');

5 Answers 5

5

I don't think you have a choice, actually.

The default parameters should be last ones on your function. Otherwise, when you call newDate(1, 2), PHP will turn it into newDate(1, 2, null) where you may have wanted newDate(1, null, 2).

See PHP's documentation on function arguments.

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

Comments

3

It better to define default parameters at the end of function declaration..

when having nullable or default parameter in the center, calling looks like:

newDate($someDay, null, $override);

however having default arguments at the end, provides a simpler function calling:

newDate($someDay, $override); // null is passed for $time

1 Comment

It's not a matter of style, but a matter of language support. PHP only supports optional parameters at the end of the arguments list.
2

What you've outlined (i.e.: moving the optional args after the required ones) makes sense only as long as the 'flow' of the arguments still makes sense. (i.e.: Only if they don't need to be in a specific order to make sense.) However, it's still the best means of achieving your goal.

That said, if there are potentially a large number of optional arguments, it might be an idea to accept an optional array which would contain key=>value pairs of the optional args to check for.)

(Incidentally, in your first example, your should really pass null instead of ''.)

Comments

0

The issue isn't parameters that can be null. Any parameter can be null, just by passing it to the function. What's causing the difficulty here is the default value. Parameters with a default value have to be at the end since it's simpler for the compiler to assume that anything that comes before them is part of the fixed arguments.

Comments

0

The current best practice for functions that can have many optional arguments is, to the knowledge of the answerer at the time of writing, to fake named arguments using an array.

function foo($options = array()) {
  $options = array_merge(array(
    'foo' => 'defaultFoo',
    'bar' => 'defaultBar',
    'fuz' => false,
    'buz' => null,
  ), $options);

  if ($options['fuz']) {
    return $options['foo'];
  }

  return $options['foo'] . $options['bar'] . $options['buz'];
}

This is verbose to first write, but very readable when using the function and a pleasure to extend. This efficacy of this approach is demonstrated by its extensive use in the Symfony 1.4 framework, and in the way JQuery parallels it with JavaScript objects.

There is some overhead due to array_merge and array lookups.

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.