小编ale*_*tra的帖子

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

我正在实现一个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).

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

谢谢你的解释!

php scalar return-type

6
推荐指数
1
解决办法
353
查看次数

标签 统计

php ×1

return-type ×1

scalar ×1