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