2

I am looking for the exact type of a Required Parameter in a PHP Function signature

Does initializing a parameter only with NULL makes it an optional? i.e

function foo($optional=NULL, $OptionalOrRequired="Optional Or Required", $required){}

I am confused about the 2nd argument, does it comes in Required or Optional parameter?

UPDATE

I am using reflection to get all and required parameters of a function

public function getPlayer($params=3, $c){}
// results
$reflection->getNumberOfParameters()            ->   2
$reflection->getNumberOfRequiredParameters()    ->   2 // it should be one

public function getPlayer($params=3, $c, $x=NULL)
// results
$reflection->getNumberOfParameters()            ->   3
$reflection->getNumberOfRequiredParameters()    ->   2

As i get in one answer that defaults comes before required, is this the reason the reflection function is returning wrong count for required parameters?

Thanks

3
  • Basically, you omit the right hand side of the parameter from the function declaration, i.e = null then it's required, otherwise if not supplied, the parameter assumes the default value you specified on the right of the parameter declaration. Commented May 30, 2012 at 10:07
  • thanks for a quick reply Gavin, am actually confused about this NULL vs actual value that i used in 2nd parameter. in the given method signature is 2nd parameter optional or required? Commented May 30, 2012 at 10:10
  • Optional, as you have provided the default value for it, should the user not provide a value when calling it. Commented May 30, 2012 at 10:16

3 Answers 3

3

'Optional' arguments are just arguments with a default value, whether that value is null, false, a string (etc.) does not matter - if a function argument has a default value it is optional.

However, it wouldn't make sense to have an optional parameter come before a required parameter, as you must give the prior arguments some value (even if it's null) in order to 'get to' the required argument - so all arguments before the last 'required' argument are effectively required.

Some examples:

// Bad
function bad($optional = 'literally anything', $required) {}

bad('required arg');              // This breaks - 'missing argument 2'
bad('something', 'required arg'); // This works - both parameters are needed

// Good
function($required, $optional = 'literally anything') {}

good('required arg');              // This works just fine, last argument has a default
good('required arg', 'something'); // Also works fine, overrides 'literally anything'

Update RE: Reflection

As noted above, putting an 'optional' parameter before a required parameter effectively makes that 'optional' parameter required, as a value must be given for it in order to ever satisfy the method signature.

For example, if the first argument has a default and second argument does not (like in the bad function above), we NEED to pass a second argument, and so we NEED to pass a first argument also, so both are 'required'. If there are subsequent arguments with default values, still only the first 2 arguments are required.

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

Comments

1

You cannot have a required parameter coming after optional parameters in PHP. The optional parameters always have to follow the required ones (if any). Any parameter that has a default value provided (e.g. $param = 'default') is optional and any parameter without a default value is required.

Comments

-1

Further to my comment.

function test($required, $optional = null)
{

}

test(); // throws an error stating $required is missing
test('a'); // sets $required to 'a' and $optional is null
test('a', 'b'); // sets $required to 'a' and $optional to 'b'

1 Comment

This is not what the OP is talking about. He is confused about a function whose optional arguments come first, and then the required arguments. Ex: function test($optional = null, $required)

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.