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

标签 php return-type scalar

我正在实现一个 Iterator 接口(interface),如果我实现它返回标量(遵循引用 http://php.net/manual/en/class.iterator.php ),我得到这个错误:

TypeError: Return value of Collection::key() must be an instance of scalar, integer returned

类实现:

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 ):

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).

我的代码有什么错误吗?

感谢您的任何解释!

最佳答案

PHP 没有称为 \scalar 的类型。 PHP 只支持 those类型。你的 \scalar 类型看起来像是另一个类。

关于PHP7 : shouldn't a scalar return type declaration accept integer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966304/

相关文章:

php - 根据特定条件使用 php 回显图像

c# - 返回类型错误的 Blazor 任务

javascript - 我如何理解返回函数的函数?

scala - 在 Scala 中返回具有动态类型的序列

perl - 在标量上下文中曾经在Perl中评估哈希的原因是什么?

javascript - 防止将重复的商品添加到购物篮中

php - 如何在mysql codeigniter上做一个百分比

c# - 错误 - 必须声明标量变量 - C#

php - 在 CakePHP 3 中保存多个 ownToMany 关联

regex - Perl:标量内的赋值和字符串匹配(正则表达式)