Request for Comments: Optional Strict Type Checking for Scalars

The idea is that certain applications and/or use cases could benefit from the ability to specify the specific types that functions and methods accept as parameters. The benefits of this approach is the improved code clarity and more detailed code interrogation via reflection for the purposes of WSDL and automatic comment generation. There are also security benefits of effectively being able to perform strict type checking around input parameters.

The patch is available here: http://ilia.ws/patch/type_hint_final.txt (for PHP 5.3) The test cases around the functionality are available here: http://ilia.ws/patch/type_hint_tests.tar.bz2

The implemented functionality does “strict” type hinting, which means that if a function is declaring the parameter needing to be an integer, only an integer will be accepted, a floating point number, a string containing a number or a boolean will be rejected, resulting in an error message.

The supported type hints are as follows: (int/integer/long, float/double, string/binary, bool/boolean, resource, object)

The “object” type-hint indicates that the function and/or method is looking for an object of some-sort, rather then a specific instance of an object as the current functionality offers.

The patch also introduces additional methods into reflection, to simplify interrogation of the code.

ReflectionParameter::isInt() - Returns whether parameter MUST be a long

ReflectionParameter::isDouble() -Returns whether parameter MUST be a double

ReflectionParameter::isBool() - Returns whether parameter MUST be a boolean

ReflectionParameter::isObject() - Returns whether parameter MUST be a boolean

ReflectionParameter::isString() - Returns whether parameter MUST be a string

ReflectionParameter::isResource() - Returns whether parameter MUST be a resource

ReflectionParameter::getTypeHint() - Returns what type hint is defined for this parameter

rfc/typecheckingstrictonly.txt · Last modified: 2017/09/22 13:28 by