2016-10-10 3 views
6

나는 반복자 인터페이스를 구현하고있어 나는 그것이 (참조 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) 고려되어야 내 코드에 실수가 있습니까? 버그일까요?

설명해 주셔서 감사합니다.

+0

'public function key() : int'는 필요한 것입니다. 반환 할 스칼라 유형을 지정해야합니다. –

+0

반환 형식은 http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.types 일 수 있습니다. 들어 본 스칼라와 같은 것이 없습니다 – RiggsFolly

+0

PHP 설명서에 표시된 유형 코드에서 사용할 수있는 실제 유형이 아니라 단지 인간 독자를 설명하기위한 것입니다. 예 : 'mixed '라는 이름의 타입이 없다는 것은 함수가 여러 타입을 반환 할 수 있다는 것을 의미합니다. – Barmar

답변

5

PHP에는 \scalar으로 알려진 유형이 없습니다. PHP는 those 유형 만 지원합니다. 그리고 \scalar 유형이 다른 클래스처럼 보입니다.