PHP string type-hinting [closed]

﹥>﹥吖頭↗ 提交于 2019-12-08 15:19:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!