Last active
February 12, 2024 10:54
-
-
Save sjparkinson/517cabd36023fecbf70e to your computer and use it in GitHub Desktop.
Type hint array arguments in PHP 5.6 using variadic functions and argument unpacking.
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 | |
$run = function (callable ...$callables) { | |
foreach ($callables as $callable) { | |
$callable(); | |
} | |
}; | |
$callables = [ | |
function () { echo 'Hello '; }, | |
function () { echo 'World'; }, | |
function () { echo '!' . PHP_EOL; }, | |
]; | |
$run(...$callables); | |
$callables = [ | |
function () { echo 'Hello '; }, | |
'World!' . PHP_EOL, | |
]; | |
$run(...$callables); |
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 docs/test.php | |
Hello World! | |
PHP Catchable fatal error: Argument 2 passed to {closure}() must be callable, string given |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Limitations