首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是问号(?)类型声明之前的意思是php (?int)

什么是问号(?)类型声明之前的意思是php (?int)
EN

Stack Overflow用户
提问于 2018-03-21 18:43:39
回答 1查看 24.4K关注 0票数 51

我在他们使用的第40行的https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Output/Output.php中看到过这段代码。

代码语言:javascript
复制
public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null)
    {
        $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
        $this->formatter = $formatter ?: new OutputFormatter();
        $this->formatter->setDecorated($decorated);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-21 18:46:08

它被称为Nullable types

它将?int定义为intnull

参数和返回值的

类型声明现在可以通过在类型名称前加上问号来标记为可空。这表示NULL和指定的类型一样,可以作为参数传递,也可以作为值返回。

示例:

代码语言:javascript
复制
function nullOrInt(?int $arg){
    var_dump($arg);
}

nullOrInt(100);
nullOrInt(null);

函数nullOrInt将同时接受null和int。

参考:http://php.net/manual/en/migration71.new-features.php

票数 75
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49404191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档