PHP 5 Type Hinting
PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays
(since PHP 5.1). However, if NULL
is used as the default parameter value, it will be allowed as an argument for any later call.
The following excerpt from the above:
if NULL
is used as the default parameter value, it will be allowed as an argument for any later call.
Does the above mean:
if default parameters are to used use with type hinting, it can have only have NULL
as the default value.
i.e. the code in code1 is wrong and results in:
Fatal error: Default value for parameters with a class type hint can only be NULL
code1:
function setName ( string $name = "happ") {
...
}
Where as code in code2 is right:
code2:
function setName ( string $name = NULL) {
...
}
Why is this constraint assigned in php?