Most of my recent experience is in Java, and I was quite surprised the following PHP code did not throw a compile-time error:
<?php
class Test { }
$a = new Test('foo', true, false, 18320);
Is there any way to force this to be a syntax or compile-time error?
PHP, being a very loosely-typed dynamic language, always allows you to pass any set and any number of arguments to a function or constructor call without error, unless type hints in their definitions aren’t met by the arguments passed; for instance, this won’t work:
But even then, that’s not going to cause a syntax error either — that’s a fatal error at runtime.
The closest you can get is to enforce the checks at runtime within the function or method bodies.