php PHP7 中的可空返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33608821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:33:48  来源:igfitidea点击:

Nullable return types in PHP7

phpnullablereturn-typetype-hintingphp-7

提问by Jeroen De Dauw

PHP 7 introduces return type declarations. Which means I can now indicate the return value is a certain class, interface, array, callable or one of the newly hintable scalar types, as is possible for function parameters.

PHP 7 引入了返回类型声明。这意味着我现在可以指示返回值是某个类、接口、数组、可调用或新的可提示标量类型之一,就像函数参数一样。

function returnHello(): string {
    return 'hello';
}

Often it happens that a value is not always present, and that you might return either something of some type, or null. While you can make parameters nullable by setting their default to null (DateTime $time = null), there does not appear to be a way to do this for return types. Is that indeed the case, or am I somehow not finding how to do it? These do not work:

通常情况下,值并不总是存在,您可能会返回某种类型的内容,或者返回空值。虽然您可以通过将参数的默认值设置为 null ( DateTime $time = null)来使参数可以为空,但似乎没有一种方法可以为返回类型执行此操作。情况确实如此,还是我不知何故找不到如何去做?这些不起作用:

function returnHello(): string? {
    return 'hello';
}

function returnHello(): string|null {
    return 'hello';
}

回答by Elias Van Ootegem

PHP 7.1 Now supports nullable return types. The first RFC I linked to is the one they went for:

PHP 7.1 现在支持可为空的返回类型。我链接到的第一个 RFC 是他们选择的那个:

function nullOrString(int $foo) : ?string
{
    return $foo%2 ? "odd" : null;
}


old answer:

旧答案:

Since my comment was actually an answer to the question:

由于我的评论实际上是对问题的回答:

PHP 7 won't support nullable return-types just yet, but there's an RFCout to address just that, it aims to land in PHP 7.1. If it passes, the syntax would then affect all type-hints (both return types and type-hints):

PHP 7 目前还不支持可为空的返回类型,但是有一个 RFC来解决这个问题,它旨在登陆 PHP 7.1。如果通过,则语法将影响所有类型提示(返回类型和类型提示):

public function returnStringOrNull(?array $optionalArray) : ?string
{
    if ($optionalArray) {
        return implode(', ', $optionalArray);//string returned here
    }
    return null;
}

There's also a competing RFCto add union types, which would be able to do the same thing, but would look different:

还有一个竞争 RFC来添加联合类型,它可以做同样的事情,但看起来会有所不同:

public function returnStringOrNull(array|null $optionalArray) : string|null
{
    if ($optionalArray) {
        return implode(', ', $optionalArray);//string returned here
    }
    return null;
}

For now, though, you'll have to write:

不过现在,你必须写:

public function returnStringOrNull( array $optionalArray = null)
{
    if ($optionalArray) {
        return implode(', ', $optionalArray);
    }
}

Or just return an empty string to be consistent with the return type, and check falsy value:

或者只是返回一个空字符串与返回类型一致,并检查falsy值:

public function returnStringOrNull( array $optionalArray = null) : string
{
    if ($optionalArray) {
        return implode(', ', $optionalArray);
    }
    return '';
}
//call
$string = $x->returnStringOrNull();
if (!$string) {
    $string = $x->returnStringOrNull(range(1, 10));
}

回答by the_nuts

Nullable Typesare available in PHP 7.1.

可空类型在 PHP 7.1 中可用。

This is a syntax example:

这是一个语法示例:

public function getName(): ?string
{
    return $this->name; // name can be null
}

PHP 7.1 is now GA and you can upgrade from PHP 7.0 (there are only few backward incompatible changesthat you have to check)

PHP 7.1 现在是 GA,您可以从 PHP 7.0 升级(只有少数向后不兼容的更改需要检查)

回答by Bayma Bruno

It works with any type.
Example:

它适用于任何类型。
例子:

public function getOpportunity(): ?Opportunity
{
    return $this->opportunity;
}