I would like to check the type of a parameter matches, before I enter a the function.
function ((int) $integer, (string) $string ) { /*...*/ }
As opposed to
function ( $int, $string ) {
$string = (string) $string;
$int = (int) $int;
}
Is there a way to do this? I’ve also speculated in doing it as object
function ( Integer $int ) { /*...*/ }
By doing this I could send a functionName ( new Integer ( $int )); but I would love to not have the added syntax.
You certainly can use so-called type hinting with complex types (arrays, interfaces and objects) – but not with primitives (and, to my mild surprise, with traits as well):
While there’s a lot of proposals to add the scalar type hinting to PHP (there’s even an informal patch for that), it’s not that easy. I’d recommend checking out this article, as it sums up the potential pitfalls of different approaches pretty well.
P.S. BTW, it looks like PHP 5.5 might use that “check-and-cast” type hinting. Not to say I’m surprised…