Is there a way to force a function or method to be called using named parameters only?

Viewed 35

For example, let's say we wrote the following function:

function myFunction(
    Foo $foo,
    Bar $bar,
) {}

In the future, we realize we want to re-order our parameters:

function myFunction(
    Bar $bar,
    Foo $foo,
) {}

If this function is only ever called using named parameters, then the above change does not break any existing code.

Therefore, is there a way to force a function or method to be called using named parameters only?

1 Answers

If you really want to, then one way would be to use an array parameter

function myFunction ($par)
{ 
  $Foo = $par['Foo'];
  $Bar - $par['Bar'];
}

call it

$param = ['Foo'=>23.4, 'Bar'=>'Hello'];
myFunction ($param);
Related