php void 作为返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29792827/
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-26 00:35:12  来源:igfitidea点击:

Void as return type

phpvoidreturn-typephp-7

提问by Daan

I was testing return types with PHP 7.

我正在使用 PHP 7 测试返回类型。

I've created a simple script to test return types of PHP 7:

我创建了一个简单的脚本来测试 PHP 7 的返回类型:

<?php

Class Obj {

    public function __construct(){

    }

    public function test(): string { //a string needs to be returned
        return "ok";
    }

}

function foo(): Obj { //instance of Obj needs to be returned
    return new Obj();
}

$o = foo();
echo $o->test(); // output: ok

Now in other programming languages when you specify a return type voidit means you cannot return anything or you will get an error. So I wrote this script:

现在在其他编程语言中,当您指定返回类型时,void这意味着您不能返回任何内容,否则您将收到错误消息。所以我写了这个脚本:

<?php

    function foo(): void {

    }

    foo(); 

Now in above script the expected output is nothing. Instead it gives me a Fatal error:

现在在上面的脚本中,预期的输出什么都没有。相反,它给了我一个致命错误:

Fatal error: Return value of foo() must be an instance of void, none returned on line 2

致命错误:foo() 的返回值必须是 void 的实例,第 2 行没有返回

My question is (I couldn't find it), in PHP 7 will there be a similar voidtype?

我的问题是(我找不到),在 PHP 7 中会有类似的void类型吗?

回答by Siguza

Edit:

编辑:

A new separate RFC for a void return typehas been published, has passed the vote, and was implemented in PHP 7.1.
There is now a voidreturn type in PHP. :)

一个针对void 返回类型的新的单独 RFC已经发布,已经通过投票,并在 PHP 7.1 中实现。PHP
现在有一个void返回类型。:)

Original Post:

原帖:

Taken from wiki.php.net:

摘自wiki.php.net

Future Work

Ideas for future work which are out of the scope of this RFC include:

  • Allow functions to declare that they do not return anything at all (void in Java and C)

未来的工作

超出本 RFC 范围的未来工作的想法包括:

  • 允许函数声明它们根本不返回任何内容(Java 和 C 中的 void)

So currently there is no way to declare that you don't return anything.
I don't know what's best in your situation, but I'd probably just go with not declaring the return type for now.

所以目前没有办法声明你不返回任何东西。
我不知道在你的情况下什么是最好的,但我可能暂时不声明返回类型。

To answer your question whether there will be a voidreturn type in PHP 7:
There is no guaranteeyet, but I think it is very likely that voidor a synonym will be implemented in some way.

回答您的问题void,PHP 7 中是否会有返回类型:
目前还不能保证,但我认为很可能void会以某种方式实现或同义词。

回答by Levi Morrison

Author of the return types RFC here. In PHP 7.0 there will not be voidreturn types since the RFCdidn't add it and neither did any other RFC targeting PHP 7.0.

此处返回类型 RFC 的作者。在 PHP 7.0 中不会有void返回类型,因为RFC没有添加它,任何其他针对 PHP 7.0的 RFC也没有添加它。

The type voidcan exist in the PHP 7 series if we decide that adding new key/reserved words is okay for minor releases even though they will break code. This is somewhat uncertain, but it was done in PHP 5.4 with the callablekeyword.

void如果我们决定为次要版本添加新的键/保留字是可以的,即使它们会破坏代码,该类型也可以存在于 PHP 7 系列中。这有点不确定,但它是在 PHP 5.4 中使用callable关键字完成的。



Personally, I don't think we need void; we already have null. From the manual:

就个人而言,我认为我们不需要void;我们已经有了null从手册

The special NULL value represents a variable with no value. NULL is the only possible value of type null.

特殊的 NULL 值表示一个没有值的变量。NULL 是 null 类型的唯一可能值。

In PHP a function which doesn't return anything will implicitly return null. This means that you cannot ever actuallyreturn nothing*. Going the nullroute means that there are no backwards compatibility breaks since nullwill not be a valid class/interface/trait name starting in PHP 7.0and doesn't add any new key or reserved words.

在 PHP 中,不返回任何内容的函数将隐式返回null. 这意味着您实际上永远无法返回任何内容*。走这null条路线意味着没有向后兼容性中断,因为null从 PHP 7.0 开始不会是有效的类/接口/特征名称,并且不会添加任何新的键或保留字。

*People familiar with the Zend Engine will realize that you can return nothing, but if you returned nothing the variable you are assigning will be assigned null, which makes them logically equivalent.

*熟悉 Zend 引擎的人会意识到您可以不返回任何内容,但是如果您不返回任何内容,则您分配的变量将被分配为 null,这使得它们在逻辑上是等效的。



In PHP 7.1 there will be a voidpseudo-type. It is defined in the Void Return Type RFC.

在 PHP 7.1 中会有一个void伪类型。它在Void Return Type RFC 中定义。

Personally I'm sad about this because the RFC author had previously "quit" and I had picked up the RFC. Then next thing I know it's proposed and in discussion and she wouldn't wait for me to propose union types, which would have been the counterpart to void as noted above. Oh well.

就我个人而言,我对此感到难过,因为 RFC 作者之前已经“退出”,而我已经拿起了 RFC。然后接下来我知道它已被提议并正在讨论中,她不会等我提议联合类型,这将与上面提到的 void 对应。那好吧。

回答by acrobat

The voidreturn type was accepted for php 7.1. So it will come in the future.

void返回类型被接受为PHP 7.1。所以它会在未来出现。

Some examples on how it will work:

关于它将如何工作的一些示例:

function should_return_nothing(): void {
    return 1; // Fatal error: A void function must not return a value
}

function returns_null(): void {
    return null; // Fatal error: A void function must not return a value
}
function lacks_return(): void {
    // valid
}
function returns_nothing(): void {
    return; // valid
}

See the RFC from Andrea Fauldsfor more info!

有关更多信息,请参阅Andrea FauldsRFC

回答by Sanjay Kumar N S

There is no equivalent type for void in php, return NULL; may fits your requirement since it doesn't have any type like 0 or any other value. Note: actual void means no return.

php中没有void的等价类型,返回NULL;可能符合您的要求,因为它没有任何类型,如 0 或任何其他值。注意:实际无效意味着没有回报。

回答by Emanuele Del Grande

@BeNice I understand your point, anyhow I summarize the consideration from Levi Morrison as a practical question of sustainability: introducing voidas a possible return type infact, we break the assumption that the only possible type of nullis null.

@BeNice 我理解你的观点,无论如何,我将 Levi Morrison 的考虑总结为一个关于可持续性的实际问题:void作为可能的返回类型事实引入,我们打破了唯一可能类型null是的假设null

This way, voidshould be returned for the type check of null, changing architecture constraints by design and causing a mess in backward compatibility.

这样,void应该为 的类型检查返回,null通过设计更改架构约束并导致向后兼容性混乱。

// your choice implies this comparison should be true:
gettype(null) === void;

I think who used nullnot frequently in his code would bear the voidtype implementation.

我认为谁null在他的代码中使用不频繁的人会承担void类型实现。