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 solution to do like that.
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 need to change just one name for every param
See the answer for more information and implementation
Do it one 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;
}
}
Do it for each function that required 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); }
}
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)
);