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:
See the answer for more information and implementation
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.
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.
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.
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.
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)
);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With