【问题标题】:php 7.0 typehint : Fatal error with integer and default valuephp 7.0 typehint:带有整数和默认值的致命错误
【发布时间】:2017-04-22 16:11:35
【问题描述】:

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

PHP 致命错误:在第 23 行的 xxx.php 中,类类型参数的默认值只能为 NULL

错误在这个函数中:

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

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

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

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

我用的是 php 7.0.8

【问题讨论】:

  • integer 未记录在 php.net/manual/en/…。我认为他们不应该允许 (integer) 进行类型转换以避免这样的混淆。
  • 所以我想boolboolean 是一样的
  • 我会这么认为。 PHP 中的实际关键字是 bool 和 int。 php.net/manual/en/reserved.other-reserved-words.php
  • 我花了 ½ 小时弄清楚我有 typeHinted boolean 而不是 bool 让 PHP 认为应该在当前命名空间中加载一些类 «boolean»。

标签: php php-7


【解决方案1】:

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

Class name
Interface name
self
array
callable
bool
float
int
string

还有这个:

警告

不支持上述标量类型的别名。相反,他们 被视为类或接口名称。例如,使用布尔值作为 参数或返回类型将需要参数或返回值 这是类或接口布尔值的实例,而不是 输入布尔值。

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

【讨论】:

  • 我在文档中错过了...它没有写在“新功能”页面中
猜你喜欢
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 2013-03-15
  • 2013-01-13
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
相关资源
最近更新 更多