Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named Parameters solution in PHP 7+

How can I implement the named argument feature in PHP 7+?

The ideal syntax is:

find($wildcard, relative = true, listIfEmpty = false) {
    ...
}

But there is no such solution. In the answer, I implemented the shortest possible solution that supports:

  • Refactoring
  • Type Hints for the params and function result
  • Reusable param set
  • Typesafe params
  • Default values for params
  • Getter for each param ( no hard-coded string )
  • Very simple setter/getter that you only change one name for each param

See the answer for more information and implementation

like image 581
Behnam Avatar asked Jun 02 '19 12:06

Behnam


People also ask

Does PHP support named parameters?

In PHP 8.0, Named Parameters support is added, which means it's now possible to call a function/method by setting the parameters by their name.

What is named parameter in php?

Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent and allows skipping default values arbitrarily.

What is variable parameter in php?

PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do so, you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable length argument since PHP 5.6.

What is PHP default argument?

PHP allows you to define C++ style default argument values. In such case, if you don't pass any value to the function, it will use default argument value.


2 Answers

First, do it once in your Framework, or globally:

// the superclass that every params class will extend this
class params {
  protected function setter($key, $value) {
    if ($value === null) {
      return $this->{$key};
    }

    $this->{$key} = $value;
    return $this;
  }
}

Then, do this for each function that requires named arguments with proper names:

// define a helper method for simple instantiation
function params_find() { return new params_find(); }

// a params class that will use from find() method
class params_find extends params {

  // list of params with default values
  // below list is just a sample
  protected $listIfEmpty   = false;
  protected $structured    = true;
  protected $relative      = false;
  protected $prefix        = "";
  protected $justFileNames = false;

  // for each param duplicate line and just change function name exactly matching param name. setter/getter will done automatically
  function listIfEmpty($val = null)   { return $this->setter(__FUNCTION__, $val); }
  function structured($val = null)    { return $this->setter(__FUNCTION__, $val); }
  function relative($val = null)      { return $this->setter(__FUNCTION__, $val); }
  function prefix($val = null)        { return $this->setter(__FUNCTION__, $val); }
  function justFileNames($val = null) { return $this->setter(__FUNCTION__, $val); }
}

After that, write your function using this feature:

// your function that uses named arguments
// in this example $wildcard argument is required
function find($wildcard = [], params_find $opt = null) {
  if ($opt === null) {
    $opt = params_find();
  }

  // do something with your params like this
  if ($opt->structured()) {
    // ...
  }

  return $something_if_you_want;
}

Finally, use it with the simplest interface:

// finally use your function with some params ( other params will have default values )
$files = find("*.css", params_find()
  ->structured(false)
  ->listIfEmpty(true)
  ->prefix("something")
);


// if you need all default values
$files = find("*.css");


// reusable options
$opt = params_find()
  ->listIfEmpty(false)
  ->prefix("something")
)
$files1 = find("*.css", $opt);
$files2 = find("*.html", $opt);
$files3 = find("*.scss", $opt
  ->structured(true)
);
like image 120
Behnam Avatar answered Oct 19 '22 00:10

Behnam


It's a PHP 8 >= feature now: https://wiki.php.net/rfc/named_params

<?php
// Using positional arguments:
array_fill(0, 100, 50);
 
// Using named arguments:
array_fill(start_index: 0, num: 100, value: 50);
like image 30
celsowm Avatar answered Oct 18 '22 23:10

celsowm