Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php 7.0 typehint : Fatal error with integer and default value

Tags:

php

php-7

I just updated my app to use php7 typehints for scalar types. When i run my unit tests, i got this error :

PHP Fatal error: Default value for parameters with a class type can only be NULL in xxx.php on line 23

The error is in this function :

public function call(string $url, integer $timeout = 30)
{
    // some code...
}

If i replace integer with int the error disappears. I always heard that int and integer is the same I don't see anything related to this in the documentation...

The php error seems to say that integer is a class and not a scalar type.

Moreover, ((int) 1) === ((integer) 1) returns true suggesting again that int and integer are the same

I use php 7.0.8

like image 699
ᴄʀᴏᴢᴇᴛ Avatar asked Dec 07 '16 16:12

ᴄʀᴏᴢᴇᴛ


1 Answers

According to the documentation these are the valid types:

Class name
Interface name
self
array
callable
bool
float
int
string

Also there's this:

Warning

Aliases for the above scalar types are not supported. Instead, they are treated as class or interface names. For example, using boolean as a parameter or return type will require an argument or return value that is an instanceof the class or interface boolean, rather than of type bool.

Source: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

like image 130
Arnolio Avatar answered Oct 03 '22 18:10

Arnolio