1

How may I use this function and inform only the first and the last arguments?

Function

function foo($first = false, $second = false, $third = false, $last = false)
{
  if($first && $last)
  {
    echo 'ok';
  }
}

I've tried the code below, but it didn't work...

foo($first = true, $last = true);
William
  • 1,010
  • 3
  • 21
  • 39

3 Answers3

2

PHP doesn't do named arguments as python does. See this question for more info.

However, life can be made easier by using other techniques like...

Modify the signature of the function to accept arguments as an associative array

Function

function foo($parameters)
{
  // Provide default values if parameters are not specified
  $first = isset($parameters['first']) ? $parameters['first'] : false;
  $second = isset($parameters['second']) ? $parameters['second'] : false;
  $third = isset($parameters['third']) ? $parameters['third'] : false;
  $last = isset($parameters['last']) ? $parameters['last'] : false;

  if($first && $last)
  {
    echo 'ok';
  }
}

Call

foo(['first' => true, 'last' => true]);

This way is suitable when you have a number of parameters big and variative enough and you have a complex logic inside the function so that writing all this code pays off.

It is not very convenient, however, because the default values are specified not in an obvious way, there's extra code and it's hard to track parameter usages.

Modify the signature of the function to accept a parameter object which holds all the necessary info

This is the way to go for complex signatures and especially if you have a cascade of methods which use the same arguments. I love it because it solved a big problem with passing up to 10 query parameters through processing pipeline. Now it's just one object with possibility to find every parameter usage and friendly autosuggestion of available parameters when typing ->.

Parameter object class

class ParameterObject
{
    public $first = false;
    public $second = false;
    public $third = false;
    public $last = false;
}

Function

function foo(ParameterObject $paramObj)
{
  if($paramObj->first && $paramObj->last)
  {
    echo 'ok';
  }
}

Call

$paramObj = new ParameterObject();
$paramObj->first = true;
$paramObj->last = true;

foo($paramObj);

Note! You can modify the object to use method for setting parameters which will provide you with possibility of chaining if you return $this in every set method. So the function call would like like this:

$paramObj = new ParameterObject();
foo($paramObj->setFirst(true)->setSecond(true));
Community
  • 1
  • 1
BVengerov
  • 2,947
  • 1
  • 18
  • 32
0

maybe

foo(true, false, false, true);

or change the position of arguments like

function foo($first, $last, $second=false, $third=false)
foo(true, true);

?

Frankich
  • 842
  • 9
  • 19
  • I guess `foo(true, false, false, true);` is the correct answer because I can't change the position of the `$second` ans `$third` arguments. I'll accept it if there is no other solution. Thanks! – William Nov 10 '16 at 15:49
0

if you want to use last argument of function in php you must enter all argument before it and you can't use name of arguments when call functions. in some language like swift can call function with name of argument but not in php

sadegh
  • 43
  • 3
  • 12