2 ms·
PHP has had type hints for classes for more than a decade now: function foobar(MyClass $foo) { // do stuff } foobar(new NotMyClass()); // throws an
by TazeTSchnitzel 12y ago
PHP has had type hints for classes for more than a decade now:
function foobar(MyClass $foo) {
// do stuff
}
foobar(new NotMyClass()); // throws an error
This just adds type hints for the scalar types (integer, float, string and boolean).
Since PHP has a long tradition of weak typing and this is what PHP's built-in and extension functions use, weak is the default behaviour, allowing some conversions:
function foo(int $x) {
var_dump($x);
}
foo("12"); // produces int(12)
foo(null); // throws an error
But, you can optionally turn on a strict type-checking mode for scalars on a per-file basis, which doesn't allow conversions:
<?php
declare(strict_types=1);
function foo(int $x) {
var_dump($x);
}
foo("12"); // throws an error
foo(null); // throws an error