私はIteratorインターフェイスを実装していると私はそれが(参照http://php.net/manual/en/class.iterator.php以下)のスカラを返す実装した場合、私はこのエラーを得た:PHPによるとPHP7:スカラー型の型宣言は整数を受け入れるべきではありませんか?
class Collection implements \Iterator
{
public function key(): \scalar
{
return key($this->colecao);
}
// other methods implementations...
}
:
TypeError: Return value of Collection::key() must be an instance of scalar, integer returned
クラスの実装を
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).
:参照、整数スカラー値(http://php.net/manual/en/migration70.new-features.php)考慮されるべきです私のコードに間違いがありますか?バグでしょうか?
ありがとうございました!
'public function key():int'は必要なものです。返すスカラー型を指定する必要があります。 –
戻り値の型はhttp://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.typesのみです。私が聞いたスカラーはありません – RiggsFolly
PHPドキュメントに示されている型あなたがコードで使用できる実際の型ではなく、単に人間の読者を説明するためのものです。例えば。 'mixed'という名前の型はありません。それは、関数が複数の型を返すことができるということだけです。 – Barmar