On Wed Aug 28 11:47 AM, Nikita Popov wrote:
>
> https://wiki.php.net/rfc/variadics
Interesting idea, expanding on:
function log($message, ...$options) {}
It would seem convenient to allow ...$options to be passed as a key-value array of arguments as
well:
function logA($message, ...$options[]) { echo count($options); }
logA("foo"); // 0
logA("foo", 1, 2); // 2
logA("foo", array(1,2,3)); // 3
The difference here is that variadic options is declared as an optional array, it would not support
a 'typehint' forcing all arguments to be of the same type.
It could be a way to support ~ named parameters
// requires at least 1 argument named as 'level'
function logB($message, ...$options['level']) {
echo $options['level'] .' '. count($options);
}
logB("foo"); // fails: 'level' argument
missing
logB("foo", 'notice'); //notice 1
logB("foo", ['level' => 'notice']); // notice 1
logB("foo", 'notice', 'extra'); // notice 2
logB("foo", ['level' => 'notice'], 'extra'); // notice 2
// requires min 2 arguments
function logC($message, ...$options['level','priority']) {
echo 'level:'. $options['level'];
echo 'priority:'. $options['priority'];
}
logC("foo", "notice", 4);
logC("foo", ['level' => 'notice', 'priority' => 4]);
That would remove the need for a "splat" or "scatter" operator. The declaration
"...$options[]" would mean, I accept an array of arguments followed by extra arguments