Created
May 11, 2016 03:23
-
-
Save johnkary/dfa372cf251bebb6da1a39bfe36014af to your computer and use it in GitHub Desktop.
How did PHP support variadic arguments before the `...$array` function argument syntax in PHP 5.6?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function some_old($fn) { | |
// Must remove $fn to use remaining args as variadic | |
$variadicArrays = array_slice(func_get_args(), 1); | |
// $variadicArrays is an "implicit" argument because it's not defined in the signature | |
var_dump($variadicArrays); | |
} | |
function some_new($fn, $someNewParam) { | |
// Must remove $fn and now $someNewParam before variadic argument placeholder | |
$variadicArrays = array_slice(func_get_args(), 2); | |
// In fact, $variadicArrays is an "implicit" argument because it's not defined in the signature | |
var_dump($variadicArrays); | |
} | |
$fn = function ($i) { return $i; }; | |
some_old($fn, ['one'], ['two'], ['three']); | |
some_new($fn, 'Hello', ['one'], ['two'], ['three']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment