PHP 字符串类型提示
为什么 PHP 只支持数组和对象的类型提示?我想使用如下所示的类型提示:
protected function addServiceToken(string $token)
{
if(!array_key_exists($token, $this->service_tokens))
$this->service_tokens[$token] = 'waiting';
return $this->checkServiceTokenStatus($token);
}
而不是添加不太优雅的调用 is_string($token) 来确保传递正确类型的参数。
当前 SVN 主干中有一个支持此功能的实现 PHP 版本,但尚未确定该实现是否是 一个在 PHP 的未来版本中发布的版本,或者是否会发布 完全支持。
因此,显然除了我自己之外,还有人(或很多人)想要使用这种语法,反之亦然。如果代码已经写好了,为什么有人不希望包含它,特别是因为类型提示是一个可选的选择?我个人希望看到 PHP 继续采用更多这样的功能,这些功能鼓励“良好”的编码实践,例如在使用变量之前检查变量的类型。当他们向语言添加类型提示时,他们看到排除标量类型有什么好处?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论