问题
Why does PHP only support type hinting for arrays and objects? I want to use a type hint like the following:
protected function addServiceToken(string $token)
{
if(!array_key_exists($token, $this->service_tokens))
$this->service_tokens[$token] = 'waiting';
return $this->checkServiceTokenStatus($token);
}
Instead of adding a less elegent call to is_string($token)
to ensure that the proper type of argument is passed.
@Daniel Egeberg writes in an answer to a related question:
There is currently an implementation supporting this in the SVN trunk version of PHP, but it's undecided if that implementation will be the one that gets released in future versions of PHP, or if it will be supported at all.
So, apparently someone (or somemany) besides myself would like to use this syntax and vice-versa. If the code has already been written why would anyone not want it included, particularly as type-hinting is an optional choice? I personally would like to see PHP continue to adopt more features like this which encourage "good" coding practices like checking the type of a variable before using it. What benefit did they see in excluding scalar types when they added type hinting to the language?
来源:https://stackoverflow.com/questions/8318238/php-string-type-hinting