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

ale*_*tra 6 php scalar return-type

我正在实现一个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...
}
Run Code Online (Sandbox Code Playgroud)

根据PHP参考,整数应被视为标量值(http://php.net/manual/en/migration70.new-features.php):

标量类型声明有两种形式:强制(默认)和严格.现在可以强制执行以下参数类型(强制或严格):字符串(字符串),整数(int),浮点数(浮点数)和布尔值(bool).

在我的代码中是否存在错误,您的错误是什么?

谢谢你的解释!

Grz*_*jda 5

PHP没有一种已知的类型\scalar.PHP仅支持这些类型.你的\scalar类型看起来像是另一个类.