This rfc tries to propose an alternative solution for type hinting in PHP. Basically its a weak type hinting(auto type conversion if possible without data loss), with support for scalars*, implemented as an spl interface.
* You hint the matching Spl classname instead of the scalar name, the weak type hinting will kick-in and validate/convert the passed argument into the Spl type, but it can be used as a scalar thanks to the behaviour provided by the SplType.
From the perspective of the API developer, E_STRICT or similar low priority notices cannot guarantee, that the API consumer is noticed about the invalid argument. From the perspective of the API consumer, for catching the E_STRICT is either require adding/modifying a global error handler, or using the @ to suppress the error and call error_get_last to check for error, which is just hackish.
Currently, there is no support for hinting scalar types, and only strict type hinting is allowed (there is no conversion).
The idea is that we could introduce a new Spl interface, which supports the following behaviour: - if the hinted argument type and the passed argument type is different, but the type conversion can be done without data loss, then convert the argument into the hinted type. - if the hinted argument type and the passed argument type is different, and cannot be converted without data loss, then throws a InvalidArgumentException.
class foo implements WeakTypeHinting{ public function bar(SplInt $baz){ return ++$baz; } } $foo = new foo; echo $foo->bar(1); // 2 echo $foo->bar('2'); // 3 echo $foo->bar('3a') // InvalidArgumentException
If an object implements ArrayAccess spl interface, it could be handled as an array(eg. if you hinted array, you can pass ArrayObject).