![](/img/trans.png)
[英]PHP7: Methods with a scalar type declaration refuse to type juggle NULL values, even in weak/coercive mode
[英]PHP7: shouldn't a scalar return type declaration accept integer?
我正在实现一个Iterator接口,如果我实现它返回标量(参考http://php.net/manual/en/class.iterator.php ),我得到了这个错误:
TypeError:Collection :: key()的返回值必须是标量的实例,返回整数
类实现:
class Collection implements \Iterator
{
public function key(): \scalar
{
return key($this->colecao);
}
// other methods implementations...
}
根据PHP参考,整数应被视为标量值( http://php.net/manual/en/migration70.new-features.php ):
标量类型声明有两种形式:强制(默认)和严格。 现在可以强制执行以下参数类型(强制或严格):字符串(字符串),整数(int),浮点数(浮点数)和布尔值(bool)。
在我的代码中是否存在错误,您的错误是什么?
谢谢你的解释!
PHP没有称为\\scalar
的类型。 PHP仅支持这些类型。 你的\\scalar
类型看起来像是另一个类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.