-
-
Save parweb/55647686cb342b92c54c0951fd294201 to your computer and use it in GitHub Desktop.
You may type-hint on variadic functions in PHP to enforce type on elements in the array
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 | |
class Foo {} | |
$f1 = new Foo(); | |
$f2 = new Foo(); | |
$bar = function (Foo ...$foo) { | |
foreach ($foo as $f) { | |
echo get_class($f) . "\n"; | |
} | |
}; | |
$bar($f1, $f2); | |
// Foo | |
// Foo | |
$bar($f1, 'bar', $f2); | |
// PHP Fatal error: Uncaught TypeError: Argument 2 passed to {closure}() must | |
// be an instance of Foo, string given |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment