mrkrstphr

mrkrstphr's blog

twitter github

Waiting for 5.6: Variadic Functions

Posted on 23 Sep 2013

An RFC for Variadic Functions has been voted on and accepted by PHP developers, and should make it into the next release (5.6).

Variadic functions are ones that accept a variable, or infinite, amount of arguments. There are already internal functions in PHP that support this (as C does), such as sprintf where you can pass it any number of arguments.

<?php

sprintf($format, $arg1, $arg2, $arg3);

Currently, if you wanted to support this in a user-land PHP function or method, you'd have to use the func_get_args() family of functions to determine what was passed beyond the definition. Now, with this accepted RFC, you can define a function like follows (stolen straight from the RFC):

<?php

public function query($query, ...$params) {
    $stmt = $this->pdo->prepare($query);
    $stmt->execute($params);
    return $stmt;
}

$params will be an array of every additional argument passed to that method.

Checkout the RFC for more examples and details.

* The idea for the "Waiting for..." posts graciously stolen from depesz. Checkout his fantastic PostgreSQL blog.

cleanphp

The Clean Architecture in PHP

I've written a book on the Clean Architecture in PHP, which covers how to cleanly organize and decouple your code in a way that prepares it to survive and be easy to maintain for the long term.

Check it out!