| auroraeosrose | Yes | 2013-09-16 20:38 |
| blanchonvincent | Yes | 2013-09-17 09:52 |
| bukka | Yes | 2013-09-16 21:28 |
| colder | Yes | 2013-09-16 21:18 |
| datibbaw | Yes | 2013-09-17 02:47 |
| derick | Yes | 2013-09-16 20:52 |
| dmitry | Yes | 2013-09-17 06:08 |
| drak | Yes | 2013-09-17 05:03 |
| fabpot | Yes | 2013-09-16 21:27 |
| hradtke | No | 2013-09-16 22:21 |
| indeyets | Yes | 2013-09-17 05:46 |
| jpauli | Yes | 2013-09-17 12:38 |
| jwage | Yes | 2013-09-16 21:22 |
| kalle | Yes | 2013-09-16 22:09 |
| kassner | Yes | 2013-09-23 07:59 |
| klaussilveira | Yes | 2013-09-17 15:45 |
| krakjoe | Yes | 2013-09-16 20:29 |
| leszek | Yes | 2013-09-18 07:31 |
| levim | Yes | 2013-09-17 14:18 |
| mike | Yes | 2013-09-17 06:56 |
| nikic | Yes | 2013-09-16 20:18 |
| pajoye | Yes | 2013-09-16 22:06 |
| peehaa | Yes | 2013-09-16 21:32 |
| pollita | Yes | 2013-09-16 20:33 |
| rasmus | Yes | 2013-09-16 21:42 |
| rdohms | Yes | 2013-09-16 22:23 |
| reeze | Yes | 2013-09-17 14:28 |
| remi | Yes | 2013-09-17 07:13 |
| sean | Yes | 2013-09-16 20:44 |
| seld | Yes | 2013-09-16 20:49 |
| stas | Yes | 2013-09-16 21:42 |
| toby | Yes | 2013-09-17 07:20 |
| treffynnon | Yes | 2013-09-19 16:05 |
| weierophinney | Yes | 2013-09-16 21:00 |
| wez | Yes | 2013-09-16 21:13 |
| yohgaki | Yes | 2013-09-17 19:55 |
| zeev | Yes | 2013-09-16 21:41 |
Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC
Currently variadic functions are implemented by fetching the function arguments using func_get_args(). The following code sample shows an implementation of a variadic function used to prepare and execute a MySQL query (I'll be making use of this example throughout the RFC):
class MySQL implements DB {
protected $pdo;
public function query($query) {
$stmt = $this->pdo->prepare($query);
$stmt->execute(array_slice(func_get_args(), 1));
return $stmt;
}
// ...
}
$userData = $db->query('SELECT * FROM users WHERE id = ?', $userID)->fetch();
There are two issues with the above approach:
Firstly, by just looking at the function signature public function query($query) you cannot know that this is actually a variadic function. You'd think that the function can only run a normal query and doesn't support bound parameters.
Secondly, because func_get_args() returns *all* arguments passed to the function you first need to remove the $queryparameter using array_slice(func_get_args(), 1).
This RFC proposed to solve these issues by adding a special syntax for variadic functions:
class MySQL implements DB {
public function query($query, ...$params) {
$stmt = $this->pdo->prepare($query);
$stmt->execute($params);
return $stmt;
}
// ...
}
$userData = $db->query('SELECT * FROM users WHERE id = ?', $userID)->fetch();
The ...$params syntax indicates that this is a variadic function and that all arguments after $query should be put into the $params array. Using the new syntax both of the issues mentioned above are solved.
The following example shows how the variadic parameter ...$params is populated depending on the number of passed arguments:
function fn($reqParam, $optParam = null, ...$params) {
var_dump($reqParam, $optParam, $params);
}
fn(1); // 1, null, []
fn(1, 2); // 1, 2, []
fn(1, 2, 3); // 1, 2, [3]
fn(1, 2, 3, 4); // 1, 2, [3, 4]
fn(1, 2, 3, 4, 5); // 1, 2, [3, 4, 5]
$params will be an empty array if the number of passed arguments is smaller than the number of declared parameters. Any further arguments will be added to the $params array (in the order in which they were passed). The $params array is using continuous zero-based indices.
The new syntax additionally adds support for capturing variadic arguments by-reference, something that was previously not possible in userland code. Only internal functions could make use of this via ZEND_ACC_PASS_REST_BY_REF.
This would allow implementing functions like sscanf() or mysqli_stmt::bind_param() in userland. The following method uses the new syntax to prepare a query and bind parameters by-reference to it:
class MySQL implements DB {
public function prepare($query, &...$params) {
$stmt = $this->pdo->prepare($query);
foreach ($params as $i => &$param) {
$stmt->bindParam($i + 1, $param);
}
return $stmt;
}
// ...
}
$stmt = $db->prepare('INSERT INTO users (name, email, age) VALUES (?, ?, ?)', $name, $email, $age);
foreach ($usersToInsert as list($name, $email, $age)) {
$stmt->execute();
}
A by-reference capture of variadic arguments is indicated by a & before the ellipsis ....
Furthermore it's possible to provide a typehint that all variadic arguments are checked against. E.g. this is how the signature of array_merge implemented in userland would look like:
function array_merge(array ...$arrays) { /* ... */ }
PHP would make sure that all arguments are actually arrays. This also works for all other typehints like callable ...$callbacks or Route ...$routes.
An advantage of declaring variadics in the parameter list is that the signature can be enforced by intefaces and during inheritance. The MySQL::query() and MySQL::prepare() examples above already referenced an interface DB. This is how the interface could look like:
interface DB {
public function query($query, ...$params);
public function prepare($query, &...$params);
// ...
}
This interface will force any implementation to make both these functions variadic and will also enforce the by-ref capture for prepare().
The exact protoype checks (what is valid and what is not) are outlined below:
// INVALID: Turning a variadic function into a non-variadic one
public function query($query, ...$params)
public function query($query)
// VALID: Turning a non-variadic function into a variadic one
public function query($query)
public function query($query, ...$params)
// Note: This is allowed as ...$params is optional and PHP allows additional optional arguments
// INVALID: Changing the passing mode for variadic parameters
public function query($query, &...$params)
public function query($query, ...$params)
// INVALID: Changing the typehint of a variadic parameter to an incompatible typehint
public function query($query, array ...$params)
public function query($query, callable ...$params)
// INVALID: Removing parameter before the variadic parameter
public function query($query, ...$params)
public function query(...$params)
// Note: Personally I don't think this makes sense, but this is how
// PHP behaves in general, so I'm staying consistent with it
// VALID: Adding additional optional parameter before the variadic parameter (with compatible typehint)
public function query($query, array ...$params)
public function query($query, array $extraParam = null, array ...$params)
// INVALID: Adding additional optional parameter with incompatible typehint
public function query($query, array ...$params)
public function query($query, callable $extraParam = null, array ...$params)
There may be only one variadic parameter and it needs to be the last parameter of the function. A variadic parameter may not have a default value.
As such all of the following are invalid:
function fn(...$args, $arg)
function fn(...$args1, ...$args2)
function fn($arg, ...$args = [])
Two new methods are added to Reflection:
bool ReflectionFunction::isVariadic()
bool ReflectionParameter::isVariadic()
The functions will return true if the function/parameter is variadic, false otherwise.
To sum up, the feature adds the following new syntax:
function fn($arg, ...$args): Capture all variadic arguments into the $args arrayfunction fn($arg, &...$args): Do the capture by referencefunction fn($arg, array ...$args): Enforce that all variadic arguments are arrays (or some other typehint)function fn($arg, array &...$args): Combine both - variadic arguments are arrays that are captured by-referenceThe advantages of the syntax are:
array_slice() the variadic arguments from func_get_args()This change does not break backwards compatibility for userland code.
In particular, this RFC does not propose to deprecate or remove the func_get_args() family of functions, at least not any time soon.
The pass_rest_by_ref argument of ZEND_BEGIN_ARG_INFO and ZEND_BEING_ARG_INFO_EX is no longer used. Instead functions can declare a variadic argument in the arginfo using ZEND_ARG_VARIADIC_INFO.
For example, this is how the arginfo for sscanf() changed:
// OLD:
ZEND_BEGIN_ARG_INFO_EX(arginfo_sscanf, 1, 0, 2)
ZEND_ARG_INFO(0, str)
ZEND_ARG_INFO(0, format)
ZEND_ARG_INFO(1, ...)
ZEND_END_ARG_INFO()
// NEW:
ZEND_BEGIN_ARG_INFO_EX(arginfo_sscanf, 0, 0, 2)
ZEND_ARG_INFO(0, str)
ZEND_ARG_INFO(0, format)
ZEND_ARG_VARIADIC_INFO(1, vars)
ZEND_END_ARG_INFO()
It would theoretically be possible to retain support for pass_rest_by_ref (by automatically generating a variadic arg), but as this is an exceedingly rarely used feature I don't think this is necessary. All usages of it in php-src have been replaced.
Apart from this the change should be transparent from an internals point of view. Macros like ARG_MUST_BE_SENT_BY_REF continue to work.
Presumably this RFC will quickly deteriorate towards a bike-shedding of the best syntax for variadic parameters, so I'll try to explain my choice for the proposed syntax right away:
The use of ... is already familiar from the PHP documentation, where variadics are denoted using a trailing $... parameter. The reason ... follows *before* the parameter in the proposed syntax is to clearly show that the typehint/ref-modifier before it applies to all arguments.
Some possible alternative syntax and why I don't like it:
$args.... With ref-modifier (&$args...) this does not show well that the individual arguments are references, rather than $args itself. With typehint (array $args...) it also looks like the typehint applies to $args itself rather than all variadic arguments.*$args. This is the syntax that both Ruby and Python use. For PHP this does not work well because *$ is a weird combination. It gets worse with a by-reference capture: &*$args. This looks like a random sequences of special characters. Combined with a typehint the syntax looks a lot like a pointer: Foo *$args.params $args. This is what C# does. This would require making params a keyword. Furthermore this doesn't have any nice way to declare typehints. In C# this is done using params type[] args, but PHP doesn't have type[] hints and introducing them only here doesn't seem right.The proposed syntax is also used by Java and will be used in Javascript (ECMAScript Harmony proposal). Go and C++ also employ a similar syntax.
Patch available in PR#421: https://github.com/php/php-src/pull/421
The vote started on 16.09.2013 and ended on 23.09.2013. There were 36 votes in favor and one against, as such the necessary two-third majority is met and this feature is accepted.
Vote: Should the proposed variadic-function syntax be added in PHP 5.6 (master)? — Yes: 36 · No: 1 (closed)
The argument unpacking RFC introduces the following related syntax:
$db->query($query, ...$params);