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

Junaid
  • 2,084
  • 1
  • 20
  • 30
  • 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. – Gavin May 30 '12 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? – Junaid May 30 '12 at 10:10
  • Optional, as you have provided the default value for it, should the user not provide a value when calling it. – Gavin May 30 '12 at 10:16

3 Answers3

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.

connec
  • 7,231
  • 3
  • 23
  • 26
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.

lanzz
  • 42,060
  • 10
  • 89
  • 98
-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'
Gavin
  • 6,284
  • 5
  • 30
  • 38
  • 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)` – Kayla Mar 18 '14 at 22:37