Since PHP 7.0, some of the harmful effects of type juggling can be mitigated with strict typing. By including this declare
statement as the first line of the file, PHP will enforce parameter type declarations and return type declarations by throwing a TypeError
exception.
declare(strict_types=1);
For example, this code, using parameter type definitions, will throw a catchable exception of type TypeError
when run:
<?php
declare(strict_types=1);
function sum(int $a, int $b) {
return $a + $b;
}
echo sum("1", 2);
Likewise, this code uses a return type declaration; it will also throw an exception if it tries to return anything other than an integer:
<?php
declare(strict_types=1);
function returner($a): int {
return $a;
}
returner("this is a string");