【问题标题】:PHP nullable type declaration [duplicate]PHP可为空的类型声明[重复]
【发布时间】:2021-01-19 01:19:27
【问题描述】:

这个函数在 PHP 7.4 中有什么不同吗?

public function foo(string $argumentThatCanBeNull = null) {}

public function bar(?string $argumentThatCanBeNull = null) {}

【问题讨论】:

  • 在 PHP 7.4 中你不需要= null?string $argumentThatCanBeNull 很好。
  • 如果您不想向这些函数传递任何参数,则需要它。

标签: php function methods parameters nullable


【解决方案1】:

没有区别,因为字符串默认可以为空。

顺便说一句,这是在 PHP 7.1 中引入的:https://www.php.net/manual/en/migration71.new-features.php

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 2019-03-25
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多