The next major php release is 5.6. Here are two new abilities already accepted by php community to be included into language from version 5.6.
1. Variadic functions
func_get_args()
is not needed any more!
function query($query, ...$params) { $stmt = $this->pdo->prepare($query); $stmt->execute($params); return $stmt; }
The ...$params
syntax indicates that this is a variadic function and that all arguments after $query should be put into the $params array.
By reference
function prepare($query, &...$params) { $stmt = $this->pdo->prepare($query); foreach ($params as $i => &$param) { $stmt->bindParam($i + 1, $param); } return $stmt; }
Type hint
function array_merge(array ...$arrays) { /* ... */ }
Prototype
interface DB { public function query($query, ...$params); public function prepare($query, &...$params); }
Summary. The feature adds the following new syntax:
-
function fn($arg, ...$args)
: Capture all variadic arguments into the$args
array -
function fn($arg, &...$args)
: Do the capture by reference -
function 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-reference
2. Importing namespaced functions
This is the new ability to import functions into a namespace. This feature introduces a new keyword combination use function. It works as follows:
namespace foo\bar { function baz() { return 'foo.bar.baz'; } function qux() { return baz(); } } namespace { use function foo\bar\baz, foo\bar\qux; var_dump(baz()); var_dump(qux()); }
All of this applies also to namespaced constants. For consistency, a use const sequence is introduced:
namespace foo\bar { const baz = 42; } namespace { use const foo\bar\baz; var_dump(baz); }
Just like classes, it is possible to alias imported functions and constants:
namespace { use function foo\bar as foo_bar; use const foo\BAZ as FOO_BAZ; var_dump(foo_bar()); var_dump(FOO_BAZ); }