Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm implementing an Iterator interface and if I implement it returning scalar (following the reference http://php.net/manual/en/class.iterator.php), I got this error:

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

The class implementation:

class Collection implements \Iterator
{
    public function key(): \scalar
    {
        return key($this->colecao);
    }
    // other methods implementations...
}

According to the PHP reference, integer should be considered a scalar value (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).

Is there some mistake in my code your would it be a bug?

Thank you for any explanation!

like image 919
alexandrecintra Avatar asked Oct 10 '16 20:10

alexandrecintra


1 Answers

PHP does not have a type known as \scalar. PHP only supports those types. And your \scalar type looks like it is another class.

like image 196
Grzegorz Gajda Avatar answered Oct 22 '22 18:10

Grzegorz Gajda