简体   繁体   English

PHP7:标量返回类型声明不应该接受整数吗?

[英]PHP7: shouldn't a scalar return type declaration accept integer?

I'm implementing an Iterator interface and if I implement it returning scalar (following the reference http://php.net/manual/en/class.iterator.php ), I got this error: 我正在实现一个Iterator接口,如果我实现它返回标量(参考http://php.net/manual/en/class.iterator.php ),我得到了这个错误:

TypeError: Return value of Collection::key() must be an instance of scalar, integer returned TypeError:Collection :: key()的返回值必须是标量的实例,返回整数

The class implementation: 类实现:

class Collection implements \Iterator
{
    public function key(): \scalar
    {
        return key($this->colecao);
    }
    // other methods implementations...
}

According to the PHP reference, integer should be considered a scalar value ( http://php.net/manual/en/migration70.new-features.php ): 根据PHP参考,整数应被视为标量值( http://php.net/manual/en/migration70.new-features.php ):

Scalar type declarations come in two flavours: coercive (default) and strict. 标量类型声明有两种形式:强制(默认)和严格。 The following types for parameters can now be enforced (either coercively or strictly): strings (string), integers (int), floating-point numbers (float), and booleans (bool). 现在可以强制执行以下参数类型(强制或严格):字符串(字符串),整数(int),浮点数(浮点数)和布尔值(bool)。

Is there some mistake in my code your would it be a bug? 在我的代码中是否存在错误,您的错误是什么?

Thank you for any explanation! 谢谢你的解释!

PHP does not have a type known as \\scalar . PHP没有称为\\scalar的类型。 PHP only supports those types. PHP仅支持这些类型。 And your \\scalar type looks like it is another class. 你的\\scalar类型看起来像是另一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM