我在https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Output/Output.php他们使用的第40行看到了这段代码?int.
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);
}
Run Code Online (Sandbox Code Playgroud)
Ata*_*man 29
它被称为Nullable types.
其中定义?int为int或null.
现在,通过在类型名称前加上问号前缀,可以将参数和返回值的类型声明标记为可为空.这表示除了指定的类型外,NULL还可以作为参数传递,或者分别作为值返回.
示例:
function nullOrInt(?int $arg){
var_dump($arg);
}
nullOrInt(100);
nullOrInt(null);
Run Code Online (Sandbox Code Playgroud)
函数nullOrInt将接受null和int.
参考:http://php.net/manual/en/migration71.new-features.php
| 归档时间: |
|
| 查看次数: |
8235 次 |
| 最近记录: |