gpt4 book ai didi

php 7.0 类型提示 : Fatal error with integer and default value

转载 作者:可可西里 更新时间:2023-10-31 23:58:10 28 4
gpt4 key购买 nike

我刚刚更新了我的应用程序以使用 php7 typehints 作为标量类型。当我运行我的单元测试时,我得到了这个错误:

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

错误在这个函数中:

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

如果我将 integer 替换为 int,错误就会消失。我总是听说 int 和 integer 是一样的,我在文档中没有看到任何与此相关的内容......

php 错误似乎表明整数是一个类而不是标量类型。

此外,((int) 1) === ((integer) 1) 返回 true 再次表明 int 和 integer 是相同的

我用的是 php 7.0.8

最佳答案

根据文档,这些是有效类型:

Class name
Interface name
self
array
callable
bool
float
int
string

还有这个:

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.

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

关于php 7.0 类型提示 : Fatal error with integer and default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41022174/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com