本文介绍了PHP方法参数类型提示带问号(?type)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是感觉在方法类型提示中使用问号的php(symfony/laravel)代码片段:

I just felt on pieces of php (symfony/laravel) code using question mark in method type hints :

public function functionName(?int $arg = 0)

在其他情况下,?类型不是最后一个,但是我没有找到其中的任何一个,但都没有默认值.

In other occasions the ?type was not the last one, but I did not find any of these with no default yet.

问题是,我找不到有关此的任何信息,所以我检查了:

Problem is, I cannot find any information about this, and I checked :

  • here : http://php.net/manual/en/migration70.new-features.php
  • and here : http://php.net/manual/en/migration71.new-features.php
  • and here : http://php.net/manual/en/functions.arguments.php

与7.2相同,但是由于代码仅需要7.1,所以看起来很正常.

And same with 7.2, but since the code only requires 7.1, it seems rather normal.

我也谷歌搜索并在这里搜索,但是要么没有记录在案,要么问号主题正在击败搜索引擎.

I also googled, and searched here, but either this is not documented or the question marks topic is defeating search engines.

所以我现在有点傻了,如果有人能在方法签名参数中启发我这个问号的含义,我将不胜感激.

So I feel a little dumb now, and I would really appreciate if someone could enlighten me on the signification of this question mark in method signatures arguments.

谢谢

推荐答案

这是php7.1中的新功能

It's a new feature in php7.1

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

问号表示类型提示参数(或返回值)也可以为空.

A question mark means that the type hinted parameter (or return value) is also allowed to be null.

因此在您的示例中,$ arg可以为null或任何整数.

So in your example $arg can be null or any integer.

这篇关于PHP方法参数类型提示带问号(?type)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:28