How to use strict types with variadic parameters in PHP

1 Answer

0 votes
declare(strict_types=1);

function sumInts(int ...$nums): int {
    return array_sum($nums);
}

echo sumInts(1, 2, 3);     // OK
echo sumInts(1, "2", 3);   // TypeError


/*
run:

ERROR!
6
Fatal error: Uncaught TypeError: sumInts(): Argument #2 must be of type int, string given

*/

 



answered Aug 9 by avibootz
...